49

我可以设置对象的attributedText属性UILabel吗?我尝试了以下代码:

UILabel *label = [[UILabel alloc] init];
label.attributedText = @"asdf";

但它给出了这个错误:

在“UILabel *”类型的对象上找不到属性“attributedText”

#import <CoreText/CoreText.h>不工作

4

8 回答 8

203

以下是如何在标签上使用属性文本的完整示例:

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;
}
于 2013-05-30T05:39:17.957 回答
33

不幸的是,UILabel不支持属性字符串。您可以改用OHAttributedLabel

更新:从 iOS6 开始,UILabel确实支持属性字符串。有关更多详细信息,请参阅UILabel 参考或 Michael Kessler 的回答。

于 2012-06-14T11:04:42.960 回答
12
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;
于 2014-06-11T18:06:36.160 回答
6

对于斯威夫特 4:

iOS 11 和 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
于 2018-07-03T06:26:03.217 回答
5

对于使用 swift 的人,这里有一个单行:

myLabel.attributedText = NSMutableAttributedString(string: myLabel.text!, attributes: [NSFontAttributeName:UIFont(name: "YourFont", size: 12), NSForegroundColorAttributeName: UIColor.whiteColor()])
于 2015-01-15T07:20:52.160 回答
3

所以,这是一个字符串的子字符串具有不同属性的代码。

 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;

    }
于 2015-01-20T11:41:01.673 回答
1

希望这可以帮助 ;)

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;
于 2012-06-14T11:02:02.473 回答
0
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;
于 2017-01-05T06:38:39.190 回答