使用YXHUD

这是本人自己设计的一个类,但功能很不完善,先看看效果:

源码:

YXHUD.h 与 YXHUD.m

//
// YXHUD.h
// UILabel
//
// Created by YouXianMing on 14-9-16.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface YXHUD : UIView @property (nonatomic, strong) NSString *showString; // 普通文本
@property (nonatomic, strong) NSAttributedString *showAttributedString; // 富文本
@property (nonatomic, assign) CGFloat fixWidth; // 固定的宽度
@property (nonatomic, assign) CGSize gapSize; // 间隙宽度
@property (nonatomic, assign) CGFloat cornerRadius; // 圆角 - (void)fix; @end
//
// YXHUD.m
// UILabel
//
// Created by YouXianMing on 14-9-16.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "YXHUD.h" @interface YXHUD () @property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIView *backedView; @end @implementation YXHUD - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// backedView(背景view)
_backedView = [UIView new];
_backedView.backgroundColor = [UIColor blackColor];
[self addSubview:_backedView]; // textLabel(文字label)
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_textLabel.textColor = [UIColor whiteColor];
_textLabel.numberOfLines = ; // 设置自动换行
[_backedView addSubview:_textLabel];
}
return self;
} #pragma mark - getter.setter方法 // 设置普通文本
@synthesize showString = _showString;
- (void)setShowString:(NSString *)showString
{
_showString = showString;
_textLabel.text = showString;
}
- (NSString *)showString
{
return _showString;
} // 设置富文本
@synthesize showAttributedString = _showAttributedString;
- (void)setShowAttributedString:(NSAttributedString *)showAttributedString
{
_showAttributedString = showAttributedString;
_textLabel.attributedText = showAttributedString;
}
- (NSAttributedString *)showAttributedString
{
return _showAttributedString;
} // 固定宽度
@synthesize fixWidth = _fixWidth;
- (void)setFixWidth:(CGFloat)fixWidth
{
_textLabel.frame = CGRectMake(, , fixWidth, );
_fixWidth = fixWidth;
}
- (CGFloat)fixWidth
{
return _fixWidth;
} // backedView的圆角
@synthesize cornerRadius = _cornerRadius;
- (void)setCornerRadius:(CGFloat)cornerRadius
{
_cornerRadius = cornerRadius;
_backedView.layer.cornerRadius = cornerRadius;
}
- (CGFloat)cornerRadius
{
return _cornerRadius;
} #pragma mark - 其他方法 // 计算出文本尺寸
- (CGRect)calculate
{
[_textLabel sizeToFit];
return _textLabel.bounds;
} // 最终计算出尺寸
- (void)fix
{
// 计算出文本的rect值
CGRect rect = [self calculate];
rect.size.height += _gapSize.height;
rect.size.width += _gapSize.width;
_backedView.frame = rect;
_textLabel.center = _backedView.center; self.frame = rect;
} @end

使用时候的代码:(注:里面用到的富文本为本人自己所写的类)

//
// RootViewController.m
// UILabel
//
// Created by YouXianMing on 14-9-16.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "RootViewController.h"
#import "YXHUD.h"
#import "ConfigAttributedString.h"
#import "NSString+YX.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; YXHUD *hud = [YXHUD new];
hud.fixWidth = ; // 固定Label宽度为100
hud.gapSize = CGSizeMake(, ); // 间隙宽度为10
hud.cornerRadius = .f; // 圆角值为5 NSString *str = @"Warnning:\n\nYouXianMing NoZuoNoDie";
NSArray *sets = @[
// 全局设置
[ConfigAttributedString font:[UIFont systemFontOfSize:.f]
range:[str range]], // 局部设置
[ConfigAttributedString font:[UIFont fontWithName:@"HelveticaNeue-Thin" size:.f]
range:[@"Warnning:" rangeFrom:str]],
[ConfigAttributedString foregroundColor:[UIColor yellowColor]
range:[@"Warnning:" rangeFrom:str]],
[ConfigAttributedString foregroundColor:[UIColor redColor]
range:[@"YouXianMing" rangeFrom:str]]]; NSLog(@"%@", [str createAttributedStringAndConfig:sets]);
hud.showAttributedString = [str createAttributedStringAndConfig:sets];
[hud fix]; CGRect rect = hud.frame;
rect.origin.x = ;
rect.origin.y = ;
hud.frame = rect; [self.view addSubview:hud];
} @end

可以固定位置:

可以进行复杂的设置:

可以设置富文本:

不过还不是完全品......

使用YXHUD的更多相关文章

随机推荐

  1. nginx配置负载均衡,tomcat宕机响应缓慢,自动切换的问题

    用了nginx负载均衡后,在两台tomcat正常运行的情况下,访问http://localhost 速度非常迅速,通过测试程序也可以看出是得到的负载均衡的效果,但是我们试验性的把其中一台tomcat( ...

  2. android学习-LocationManager(一)-定位方式原理解析

    参考资源:android 4种定位原理及实现——1 android使用不同的方法为应用提供位置信息. 定位的方式有三种:GPS地位(A-GPSAssistedGPS:辅助全球卫星定位系统,或者是同步G ...

  3. 使用redis的发布订阅模式实现消息队列

    配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  4. HDFS:分布式文件系统

    HDFS是GFS的简化版,它同一时刻只允许一个用户对同一文件进行追加写操作(GFS允许并发写).它适合存储大文件,并提供高吞吐量的顺序读/写访问. 它的早期版本两大问题,例如:单点失效和水平扩展不佳. ...

  5. Rails中的增删改查

      1.        rails中类与对象与SQL中表与行的关系 rails中提供了对象关系映射(ORM),将模型类映射至表,模型类的关联表名是类名小写后的复数形式,如类名Order,对应的表名为o ...

  6. mysql replace语句

    语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 REPLACE [LOW_PRIORITY | DELAYED]     [INTO] tbl_name     [ ...

  7. 【原】戏说Java

    戏说Java 本文只是个人闲余之际写的,查阅了些许资料,仅当娱乐.如有雷同,纯属巧合.   如果要学好一个东西,就应该要把他拟人化,当做一个你的好朋友,对他产生兴趣,那么你自然而然就会学习好他了.俗话 ...

  8. 《Programming iOS 7》读书笔记 - 窗体大小与状态栏

    1.从iOS7开始,状态栏开始变为透明的,根识图占领了整个窗体,包括状态栏后面的20像素高的区域,这种样式无法被改变.iOS6的状态栏是不透明的,窗体的尺寸通常会比屏幕的尺寸要小,可以设置窗体的尺寸为 ...

  9. Ubuntu Grub 配置跟新

    自从上了Grub2,就开始装逼了,配置不如以前方便了. 需要修改一些基本的配置如菜单超时之类的可以修改/etc/default/grub文件,然后使用update-grub来更新/boot/grub/ ...

  10. python学习之老男孩python全栈第九期_day016知识点总结

    '''数据类型:intbool... 数据结构:dict (python独有的)listtuple (pytho独有的)setstr''' # reverse() 反转l = [1,2,3,4,5]l ...