UILabel的AttributedString设置

本文详细介绍了如何使用Objective-C创建UILabel,并通过设置attributedText来实现文字的不同颜色、字体、下划线等样式。展示了UILabel的灵活性和强大功能。
<span style="font-size:18px;">  //定义UILabel
    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(100,100,200, 50)];
    
    //定义NSMutableAttributedString
    NSMutableAttributedString *string = [[NSMutableAttributedStringalloc]initWithString:@"123456789"];
    
    //对range设置颜色
    [string addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,3)];
    [string addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(3,3)];
    [string addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor]range:NSMakeRange(6,3)];
    
    //对range设置字体
    [string addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:30]range:NSMakeRange(0,4)];
    
    //设置下划线
    [string addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle]range:NSMakeRange(4,5)];
    
    

    
    label.attributedText = string;
    [self.viewaddSubview:label];</span>
<span style="font-size:18px;"><span style="color: rgb(29, 148, 33); font-family: FangSong; font-size: 18px; line-height: 26px;">效果如下</span>
</span>
<span style="font-size:18px;"><span style="color: rgb(29, 148, 33); font-family: FangSong; font-size: 18px; line-height: 26px;">
</span></span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值