首页 > 代码库 > NSMutableAttributedString 与UILabel 常用使用
NSMutableAttributedString 与UILabel 常用使用
第一步:NSMutableAttributedString与NSString关联
NSString *pingguString = [NSStringstringWithFormat:@"%@%@",pingguString1,pingguString2];
NSMutableAttributedString *str = [[NSMutableAttributedStringalloc]initWithString:pingguString];
第二步:设置字体颜色和大小 (NSForegroundColorAttributeName和NSFontAttributeName只支持ios6以上)
[str addAttribute:NSForegroundColorAttributeNamevalue:(id)[UIColorredColor].CGColorrange:NSMakeRange(0, pingguString1.length)];
[str addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:13]range:NSMakeRange(0, pingguString1.length)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(pingguString1.length, pingguString2.length)];
[str addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:13]range:NSMakeRange(pingguString1.length, pingguString2.length)];
第三步:NSMutableAttributedString与UILabel关联
UILabel * label = [[UILabelalloc]initWithFrame:rect];
[label setFont:[UIFontsystemFontOfSize:fonSize]];
[label setBackgroundColor:Color];
[label setTextAlignment:alignment];
[label setNumberOfLines:2];
[label setTag:tag];
[label setLineBreakMode:UILineBreakModeWordWrap];
label.attributedText = str;
[self.contentViewaddSubview:label];
[str release];