2012-06-14 167 views
41

我可以設置UILabel對象的attributedText屬性`attributedText`財產?我想下面的代碼:我可以設置`UILabel`

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

但它給這個錯誤:

Property "attributedText" not found on object of type 'UILabel *'

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

+0

使用此鏈接 http://stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – Rajneesh071

+0

我認爲這是有益的ü HTTP ://stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – Rajneesh071

回答

31

不幸的是, UILabel沒有按不支持屬性字符串。 您可以改用OHAttributedLabel

更新:由於iOS6,UILabel確實支持屬性字符串。有關更多詳細信息,請參閱UILabel reference或Michael Kessler的回答。

+37

作爲iOS 6的的,支持的UILabel經由attributedText屬性歸因字符串。 – Greg

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; 
+0

只需使用上面的代碼,它應該工作。沒有別的事要做。 – Blade

+0

給出了一個錯誤:房產「attributedText」不是類型的對象發現「的UILabel *」 – Shamsiddin

+0

嘗試導入「CoreText.framework」。然後把你的.h文件#import Blade

196

下面是如何在標籤上使用屬性文本完整的例子:

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; 
} 
+1

請記住使用rangeOfString這樣會導致錯誤,如果文本的某些部分是別人的一個子集。你最好使用NSRange自己處理範圍,然後手動計算出字符串的長度。 – owencm

+0

@owencm,你是對的。此代碼不能用於任何情況 - 特別是當文本來自網絡時。此代碼片段僅演示如何使用屬性文本以及向後兼容性... –

+0

很好的答案。但該死的是一個_obtuse_ API。 – aroth

8
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; 
5

對於使用SWIFT的人,這裏是一個班輪:

myLabel.attributedText = NSMutableAttributedString(string: myLabel.text!, attributes: [NSFontAttributeName:UIFont(name: "YourFont", size: 12), NSForegroundColorAttributeName: UIColor.whiteColor()]) 
3

如此,這裏是代碼對字符串的子字符串有不同的屬性。

NSString *[email protected]"10 people likes this"; 
    NSString *[email protected]"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
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;