label(也可以说字符串)上不同颜色 和 不同大小 的设置

这篇博客介绍了如何在iOS开发中通过创建NSMutableAttributedString,设置UILabel的文字内容,部分文字的颜色,以及特定范围内的字体大小。示例代码展示了一个UILabel,其中'求指导'三个字被设置为红色,'来呀,造作啊'设置为蓝色,且'求指导'的字体大小为15.0。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值