IOS-UI设置字体属性


Swift版本:

let str:String = "wenzi";
        let text = "\(str) button pressed"    //运用\符号可以拼接字符创;
//        创建一个属性字符创,并且初始化;
        let styleText = NSMutableAttributedString (string : text);
//        创建文字的属性:是一个字典类型;
                let Attribut = [NSFontAttributeName:UIFont.boldSystemFontOfSize(lable.font.pointSize)]
//        获取需要属相显示的字符串的范围;首先先强转为Objec-c的NSString;
        let nameRange = (text as NSString).rangeOfString(str);
//        然后给属性字符串赋值;
        styleText.setAttributes(Attribut, range: nameRange)
//        传给lable标签显示;
        lable.attributedText = styleText;
Object-c版本:

NSString *title = @"Left";
    NSString *plainText = @"Left button preesed";
    NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc]
                                             initWithString:plainText];
    //字典类型的赋值;
    NSDictionary *attributes =
    @{
      NSFontAttributeName : [UIFont boldSystemFontOfSize:_statusLabel.font.pointSize]
      };
    
    NSRange nameRange = [plainText rangeOfString:title];
    
    [styledText setAttributes:attributes range:nameRange];
    _statusLabel.attributedText = styledText;
两者的语法差别不是很大,所以带上基本都是差不多的;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值