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

后端 未结 8 891
长发绾君心
长发绾君心 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;
    
        }
    
    0 讨论(0)
  • 2020-12-22 22:36

    Here is a complete example of how to use an attributed text on a label:

    NSString *redText = @"red text";
    NSString *greenText = @"green text";
    NSString *purpleBoldText = @"purple bold text";
    
    NSString *text = [NSString stringWithFormat:@"Here are %@, %@ and %@", 
                      redText,  
                      greenText,  
                      purpleBoldText];
    
    // If attributed text is supported (iOS6+)
    if ([self.label respondsToSelector:@selector(setAttributedText:)]) {
    
        // Define general attributes for the entire text
        NSDictionary *attribs = @{
                                  NSForegroundColorAttributeName: self.label.textColor,
                                  NSFontAttributeName: self.label.font
                                  };
        NSMutableAttributedString *attributedText = 
            [[NSMutableAttributedString alloc] initWithString:text
                                                   attributes:attribs];
    
        // Red text attributes
        UIColor *redColor = [UIColor redColor];
        NSRange redTextRange = [text rangeOfString:redText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
        [attributedText setAttributes:@{NSForegroundColorAttributeName:redColor}
                                range:redTextRange];
    
        // Green text attributes
        UIColor *greenColor = [UIColor greenColor];
        NSRange greenTextRange = [text rangeOfString:greenText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
        [attributedText setAttributes:@{NSForegroundColorAttributeName:greenColor}
                                range:greenTextRange];
    
        // Purple and bold text attributes
        UIColor *purpleColor = [UIColor purpleColor];
        UIFont *boldFont = [UIFont boldSystemFontOfSize:self.label.font.pointSize];
        NSRange purpleBoldTextRange = [text rangeOfString:purpleBoldText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
        [attributedText setAttributes:@{NSForegroundColorAttributeName:purpleColor,
                                        NSFontAttributeName:boldFont}
                                range:purpleBoldTextRange];
    
        self.label.attributedText = attributedText;
    }
    // If attributed text is NOT supported (iOS5-)
    else {
        self.label.text = text;
    }
    
    0 讨论(0)
提交回复
热议问题