Can I set the `attributedText` property of `UILabel`

后端 未结 8 908
长发绾君心
长发绾君心 2020-12-22 21:52

Can I set the attributedText property of a UILabel object? I tried the below code:

UILabel *label = [[UILabel alloc] init];
label.a         


        
8条回答
  •  一个人的身影
    2020-12-22 22:35

    so,here is the code to have different properties for sub strings ,of a string.

     NSString *str=@"10 people likes this";
        NSString *str2=@"likes this";
        if ([str hasSuffix:str2])
        {
            NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:str];
    
        // for string 1 //
    
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,str.length-str2.length)];
             [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0,str.length-str2.length)];
      // for string 2 //
    
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange((str.length-str2.length),str2.length)];
            [string addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12] range:NSMakeRange((str.length-str2.length),str2.length)];
            label.attributedText=string;
        }
        else
        {
            label.text =str;
    
        }
    

提交回复
热议问题