UILabel * lable = [UILabel new];
lable.backgroundColor = [UIColor yellowColor];
lable.font = [UIFont systemFontOfSize: 20];
[self.view addSubview:lable];
lable.frame = CGRectMake(100, 100, 200, 30);
NSMutableAttributedString *attributedString = [NSMutableAttributedString new];
//1- 你好
NSAttributedString *first = [[NSAttributedString alloc]initWithString:@"你好"];
//2- 图片
NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
attachment.image = [UIImage imageNamed:@"header_cry_icon.png"];
//计算文字高度
float lineH = lable.font.lineHeight;
attachment.bounds = CGRectMake(0, -(lable.bounds.size.height - lineH)*0.5, lineH, lineH);
NSAttributedString *pic = [NSAttributedString attributedStringWithAttachment:attachment];
// 3- 文字
NSAttributedString *san = [[NSAttributedString alloc]initWithString:@"不好"];
[attributedString appendAttributedString:first];
[attributedString appendAttributedString:pic];
[attributedString appendAttributedString:san];
lable.attributedText = attributedString;
UILabel *label = [[UILabel alloc] init];
// 设置属性文字
NSString *text = @"你好\n哈哈哈";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 3)];
label.attributedText = attributedText;
// 其他设置
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:label];
self.navigationItem.titleView = label;
Snip20170303_1.png
UILabel *label = [[UILabel alloc] init];
// label.text = @"你好哈哈哈";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"你好哈哈哈"];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 3)];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
label.attributedText = text;
label.frame = CGRectMake(100, 100, 100, 25);
[self.view addSubview:label];
网友评论