UILabel*label = [[UILabelalloc]init];
label.frame=CGRectMake(100,100,200,25);
label.backgroundColor= [UIColorredColor];
label.font= [UIFontsystemFontOfSize:14];
[self.viewaddSubview:label];
//圖文混排
NSMutableAttributedString*attributedText = [[NSMutableAttributedStringalloc]init];
// 1-你好
NSAttributedString*frist = [[NSAttributedStringalloc]initWithString:@"你好"];
[attributedTextappendAttributedString:frist];
// 2-圖片
//帶有圖片的附件對象
NSTextAttachment*attachment = [[NSTextAttachmentalloc]init];
attachment.image= [UIImageimageNamed:@"header_cry_icon"];
CGFloatlineH = label.font.lineHeight;
attachment.bounds=CGRectMake(0, -((label.dc_height- lineH) *0.5-1), lineH, lineH);
//將附件對象包裝成一個(gè)屬性文字
NSAttributedString*second = [NSAttributedStringattributedStringWithAttachment:attachment];
[attributedTextappendAttributedString:second];
// 3-哈哈哈
NSAttributedString*third = [[NSAttributedStringalloc]initWithString:@"哈哈哈"];
[attributedTextappendAttributedString:third];
label.attributedText= attributedText;
一個(gè)Label顯示多行不同字體的文字
UILabel *label = [[UILabel alloc] init];
// 設(shè)置屬性文字
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 systemFontOfSize:13] range:NSMakeRange(3, 3)];
label.attributedText = attributedText;
// 其他設(shè)置
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:label];
self.navigationItem.titleView = label;