一、初始化
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
[self.view addSubview:myLabel];
二、设置文字
1、设置默认文本
NSString *text = @"标签文本";
myLabel.text = text;
效果:
2、设置标签文本
(此属性是iOS6.0之后才出现,如若不是必要,不建议使用此属性)
NSString *text = @"其实没什么";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
myLabel.attributedText = attributeString;
效果:
关键字标红的效果
NSString *keyword = @"你好";
NSString *result = @"你好世界";
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] init