【问题标题】:add bold text between text in UILabel [duplicate]在 UILabel 中的文本之间添加粗体文本 [重复]
【发布时间】:2016-05-30 03:48:24
【问题描述】:

我要显示修改过多的长文本.. 大文本包含特殊字符。每当行下方出现特殊字符时,应为粗体,并在出现 \n 时生成新标签。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

试试这个:你可以在值参数中添加任何字体样式。使用这个你可以为子字符串添加样式,使其与普通字符串不同。

NSString *strFirst = @"Anylengthtext";
NSString *strSecond = @"Anylengthtext";
NSString *strThird = @"Anylengthtext";

NSString *strComplete = [NSString stringWithFormat:@"%@ %@ %@",strFirst,strSecond,strThird];

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];

[attributedString addAttribute:NSForegroundColorAttributeName
              value:[UIColor whiteColor]
              range:[strComplete rangeOfString:strFirst]];

[attributedString addAttribute:NSForegroundColorAttributeName
              value:[UIFont fontWithName:@"Roboto-Regular" size:12]
              range:[strComplete rangeOfString:strSecond]];

[attributedString addAttribute:NSForegroundColorAttributeName
              value:[UIColor blueColor]
              range:[strComplete rangeOfString:strThird]];


self.lblName.attributedText = attributedString;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-21
    • 2013-01-31
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多