2012-06-14 47 views
41

Tôi có thể đặt thuộc tính attributedText của đối tượng UILabel không? Tôi đã thử đoạn code dưới đây:Tôi có thể đặt thuộc tính `thuộc tính` của` UILabel`

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

Nhưng nó mang lại cho lỗi này:

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

#import <CoreText/CoreText.h> không làm việc

+0

Sử dụng liên kết này http://stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – Rajneesh071

+0

tôi nghĩ rằng đây là hữu ích cho u http : //stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – Rajneesh071

Trả lời

31

Thật không may, UILabel doesn không hỗ trợ chuỗi được phân bổ. Thay vào đó, bạn có thể sử dụng OHAttributedLabel.

Cập nhật: Vì iOS6, UILabel hỗ trợ chuỗi được phân bổ. Xem UILabel reference hoặc câu trả lời của Michael Kessler bên dưới để biết thêm chi tiết.

+37

Kể từ iOS 6, UILabel hỗ trợ chuỗi được phân bổ thông qua thuộc tính attributedText. – Greg

1

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

Chỉ cần sử dụng mã ở trên và nó sẽ hoạt động. Không còn gì để làm. – Blade

+0

đưa ra lỗi: Thuộc tính "attributedText" không tìm thấy trên đối tượng thuộc loại 'UILabel *' – Shamsiddin

+0

Thử nhập "CoreText.framework". Và sau đó đặt trong tập tin .h #import của bạn Blade

196

Đây là một ví dụ hoàn chỉnh về cách sử dụng một văn bản do trên nhãn:

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

Ghi nhớ bằng cách sử dụng rangeOfString như thế này sẽ gây ra lỗi nếu một số phần của văn bản là một tập con của người khác. Bạn nên làm tốt hơn để làm việc ra phạm vi cho mình bằng cách sử dụng NSRange và làm việc ra chiều dài của chuỗi bằng tay. – owencm

+0

@owencm, bạn hoàn toàn đúng. Mã này không thể được sử dụng trong mọi tình huống - đặc biệt là khi các văn bản đến từ trang web. Đoạn mã này chỉ minh họa cách sử dụng mã được gán cùng với khả năng tương thích ngược ... –

+0

Câu trả lời hay. Nhưng damn là một API _obtuse_. – 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

Đối với những người sử dụng nhanh chóng, đây là một lớp lót:

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

như vậy, đây là mã có các thuộc tính khác nhau cho chuỗi con, của một chuỗi.

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; 
Các vấn đề liên quan