注:一般的label用来现实普通的文字。可是,你经常会遇到这样的状况:一段文字中不只有文字,也有图片,甚至文字中的某段文字与其余的文字的appearance不一致的状况,这样的一段文字就能够称得上是富文本了。label的attributedText属性就是用来接受这样的文本内容的。app
如图框架
集成Masonry框架布局
pch文件的配置atom
将通用的头文件添加到pch文件中插件
带参数的宏定义中的参数名,不能与其后的形式参数名相同(宏定义其实就是替换,将文本替换成指定的文本)3d
// redValue 不能写成red #define UIColorWithInt(redValue, greenValue, blueValue, alphaValue) [UIColor colorWithRed:(redValue)/255.0f green:(greenValue)/255.0f blue:(blueValue)/255.0f alpha:(alphaValue)]
包含alertLabel属性code
@interface ViewController () /** alertLabel */ @property (nonatomic, strong) UILabel *alertLabel; @end
建立alertLabelorm
- (void)viewDidLoad { [super viewDidLoad]; // 建立alertLabel self.alertLabel = [[UILabel alloc] init]; [self.view addSubview:self.alertLabel]; // 设置alertLabel的富文本属性 [self setupAlertLabel]; }
如果自定以控制,一般在layoutSubviews方法中布局子控件对象
/** * 布局alertLabel */ - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.centerY.equalTo(self.view); }]; }
设置alertLabel的attributedText属性blog
/** * 设置alertLabel */ - (void)setupAlertLabel { // 文本的显示样式 NSMutableDictionary *appearanceDictionary = [NSMutableDictionary dictionary]; appearanceDictionary[NSForegroundColorAttributeName] = UIColorWithInt(117, 117, 117, 1.0); appearanceDictionary[NSFontAttributeName] = [UIFont boldSystemFontOfSize:15]; // 文本内容(指定显示属性的文本) NSString *normolString = @" 莫将支付宝密码告诉他人,谢谢!"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:normolString attributes:appearanceDictionary]; // 改变文本中某段文字的现实属性 NSMutableDictionary *subAppearanceDictionary = [NSMutableDictionary dictionary]; subAppearanceDictionary[NSForegroundColorAttributeName] = [UIColor redColor]; subAppearanceDictionary[NSFontAttributeName] = [UIFont systemFontOfSize:17]; NSRange subRange = [normolString rangeOfString:@"谢谢!"]; [attributedString addAttributes:subAppearanceDictionary range:subRange]; // 添加图片 NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"alert"]; attachment.bounds = CGRectMake(0, 0, 14, 14); NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment]; [attributedString insertAttributedString:imageString atIndex:0]; // 设置alertLabel的attributedText self.alertLabel.attributedText = attributedString; // 给alertLabel添加点击事件 [self addTargetToAlertLabel]; }
UILabel对象默认是不具有与用户交互的能力,若要保证添加的手势有效果,须要是其具有与用户交互的能力
- (void)addTargetToAlertLabel { self.alertLabel.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertLabelClick:)]; [self.alertLabel addGestureRecognizer:tap]; } /** * alertLabel的点击事件 */ - (void)alertLabelClick:(UILabel *)label { NSLog(@"alertLabelClick"); }
打开“Finder”->“应用程序”->“Xcode”->"显示包内容"->"contents"->"Info.plist",拷贝如图所示内容
command+shift+G,进入指定路径文件夹:~/Library/Application Support/Developer/Shared/Xcode
“显示包内容”->“Contents”->"Info.plist", 新建Item,粘贴拷贝的字符串
重启Xcode,使用三个斜杠(///)来使用VVDocument