UILabel * buyLabel = [[UILabelalloc] initWithFrame:CGRectMake(10, purchaseBtnY, purchaseBtnW, purchaseBtnH)];
buyLabel.textAlignment =NSTextAlignmentCenter;
buyLabel.font = [UIFontsystemFontOfSize:13.0];
buyLabel.textColor = [UIColorblackColor];
NSString * buyLabelText = @"我要学习开发求指导,来呀,造作啊";
NSRange range = [buyLabelText rangeOfString: @"求指导"];
NSMutableAttributedString * str = [[NSMutableAttributedStringalloc] initWithString:buyLabelText];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor]range:range];
[str addAttributes: @{NSForegroundColorAttributeName: [UIColorcyanColor]}range:NSMakeRange(range.location+ range.length, 3)];
[str addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:15]range:range];
buyLabel.attributedText = str;
这篇博客介绍了如何在iOS开发中通过创建NSMutableAttributedString,设置UILabel的文字内容,部分文字的颜色,以及特定范围内的字体大小。示例代码展示了一个UILabel,其中'求指导'三个字被设置为红色,'来呀,造作啊'设置为蓝色,且'求指导'的字体大小为15.0。
1万+

被折叠的 条评论
为什么被折叠?



