NSString *nameStr1 = @"我是";
NSString *nameStr2 = @"中国人";
NSString *nameStr = [NSString stringWithFormat:@"%@ %@",nameStr1 , nameStr2] ;
//先查找分段样式文字的range
NSRange range1 = [[str string] rangeOfString:nameStr1];
NSRange range2 = [[str string] rangeOfString:nameStr2];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:nameStr];
//设置分段文字字体
[str addAttribute:NSFontAttributeName value:kTxtBond(18) range:range1];
[str addAttribute:NSFontAttributeName value:kTxtFont(14) range:range2];
//分段文字的颜色
[str addAttribute:NSForegroundColorAttributeName value:kTxtColor_Black range:range1];
[str addAttribute:NSForegroundColorAttributeName value:kTxtColor_Gray range:range2];
//因为第二段的文字略小,而 NSMutableAttributedString 默认是底部对齐的,所以让range2的文字向上偏移1像素。
//Set small string baseline offset
[str addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:1.0] range:range2];
textLabel.attributedText = str;
转自:http://www.zimuwu.cn/itarticle/ios/299.html
网友评论