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

后端 未结 8 890
长发绾君心
长发绾君心 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:15
    NSString *str1 = @"Hi Hello, this is plain text in red";
    
    NSString *cardName = @"This is bold text in blue";
    
    NSString *text = [NSString stringWithFormat:@"%@\n%@",str1,cardName];
    
    
    // Define general attributes for the entire text
    NSDictionary *attribs = @{
                              NSForegroundColorAttributeName:[UIColor redColor],
                              NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12]
                              };
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attribs];
    
    
    UIFont *boldFont = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
    NSRange range = [text rangeOfString:cardName];
    [attributedText setAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor],
                                    NSFontAttributeName:boldFont} range:range];
    
    
    myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    
    myLabel.attributedText = attributedText;
    
    0 讨论(0)
  • 2020-12-22 22:17
    UIFont *font = [UIFont boldSystemFontOfSize:12];
    NSDictionary *fontDict = [NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@" v 1.2.55" attributes: fontDict];
    
    UIFont *fontNew = [UIFont boldSystemFontOfSize:17];
    NSDictionary *fontDictNew = [NSDictionary dictionaryWithObject: fontNew forKey:NSFontAttributeName];
    NSMutableAttributedString *attrStringNew = [[NSMutableAttributedString alloc] initWithString:@“Application” attributes: fontDictNew];
    [attrStringNew appendAttributedString: attrString];
    self.vsersionLabel.attributedText = attrStringNew;
    
    0 讨论(0)
  • 2020-12-22 22:23

    for Swift 4:

    iOS 11 and xcode 9.4

      let str = "This is a string which will shortly be modified into AtrributedString"
    
      var attStr = NSMutableAttributedString.init(string: str)
    
      attStr.addAttribute(.font,
                    value: UIFont.init(name: "AppleSDGothicNeo-Bold", size: 15) ?? "font not found",
                    range: NSRange.init(location: 0, length: str.count))
    
      self.textLabel.attributedText = attStr
    
    0 讨论(0)
  • 2020-12-22 22:25

    For people using swift, here's a one-liner:

    myLabel.attributedText = NSMutableAttributedString(string: myLabel.text!, attributes: [NSFontAttributeName:UIFont(name: "YourFont", size: 12), NSForegroundColorAttributeName: UIColor.whiteColor()])
    
    0 讨论(0)
  • 2020-12-22 22:26

    Hope this helps ;)

    NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"asdf"];
    [attrStr setFont:[UIFont systemFontOfSize:12]];
    [attrStr setTextColor:[UIColor grayColor]];
    [attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,5)];
    lbl.attributedText = attrStr;
    
    0 讨论(0)
  • 2020-12-22 22:33

    Unfortunately, UILabel doesn't support attributed strings. You can use OHAttributedLabel instead.

    Update: Since iOS6, UILabel does support attributed strings. See UILabel reference or Michael Kessler's answer below for more details.

    0 讨论(0)
提交回复
热议问题