2013-05-08 40 views
5

Tôi có một chuỗi như,Làm thế nào để hiển thị màu sắc khác nhau và các loại phông chữ trong một UILabel

NSSting *[email protected]"First Second Third Fourth Fifth Sixth Seventh"; 

Tôi muốn hiển thị chuỗi này trong nhãn đơn với các loại phông chữ khác nhau và màu sắc.

tức là ..,

Chuỗi "đầu tiên" phải ở dạng Đậm.

Chuỗi "Thứ hai" phải ở dạng nghiêng.

Chuỗi "Thứ ba" phải có màu đỏ.

Chuỗi "thứ tư" phải có màu xanh lục.

Chuỗi "Thứ năm" phải có phông chữ In nghiêng đậm của Hệ thống.

Chuỗi "Thứ sáu" phải được in đậm.

"Seventh" Chuỗi nên arial đậm kích thước 34.

Tôi đã trải qua một số các câu trả lời trong stackoverflow, nhưng tôi đã không nhận được bất kỳ rõ nét trong những câu trả lời ..

Can bất kỳ một cung cấp một số hướng dẫn.

Cảm ơn trước.

Giải pháp: Trước hết thao tác NSAttributedString theo yêu cầu của bạn.

sử dụng NSAttributedString u có thể làm điều đó trong iOS 6 UILabelattributedString property.

Below ios 6.0 sử dụng TTAtributeLabel hoặc bất kỳ third party attributedLabel cho displaying attributedString.

+0

Bạn không thể làm điều đó trong một nhãn. 7 nhãn khác nhau you'l cần –

+1

sử dụng NSAttributedString u có thể làm điều đó trong ios6 uilabel có thuộc tính attributedString. –

+0

Dưới đây ios 6.0 sử dụng TTAtributeLabel để hiển thị attributedString. –

Trả lời

1

Trong iOS 6.0 trở lên UILabel có một tài sản:
@property(nonatomic,copy) NSAttributedString *attributedText
chấp nhận một NSAttributedString.

Bạn có thể tạo NSAttributedString khớp với yêu cầu của bạn.

+0

cái nào nhỏ hơn iOS 6.0 ???? –

0

Hãy thử nó ....

DAAttributedStringUtils

và cũng thấy this

Different Label

Hope i giúp

+0

kiểm tra http://soulwithmobiletechnology.blogspot.in/2012/07/how-to-use-nsattributedstring-in-ios-6.html –

+0

@Prince: Bạn nói gì? hãy giải thích chi tiết –

3

Sử dụng mã này cho thiết lập màu sắc khác nhau để từ khác nhau. .

NSString *test = @"First Second Third Fourth Fifth Sixth Seventh"; 

CFStringRef string = (CFStringRef) test; 
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string); 



    /* 
    Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its 
    attributes right when we create it. We can change them if we use mutable form of CFAttributeString. 
    */ 



    //Lets choose the colors we want to have in our string 
    CGColorRef _orange=[UIColor orangeColor].CGColor; 
    CGColorRef _green=[UIColor greenColor].CGColor; 
    CGColorRef _red=[UIColor redColor].CGColor; 
    CGColorRef _blue=[UIColor blueColor].CGColor;  




    //Lets have our string with first 20 letters as orange 
    //next 20 letters as green 
    //next 20 as red 
    //last remaining as blue 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange); 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green); 
    CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);  
    CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue); 

Ngoài ra, bạn có thể đặt Màu bằng hình ảnh như dưới đây ..

[yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]]; 

Xem Một trả lời của tôi Từ This Link

tôi hy vọng điều này là hữu ích cho bạn ...

1

nếu u không sử dụng NSAttributeLabel sau đó hãy thử điều này, các công trình của nó tốt cho tôi

thêm phương pháp này vào lớp CommonFuction & thêm khuôn khổ này CoreText.framework

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString label:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName 
{ 
    label.layer.sublayers = nil; 

    NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text]; 

    for (int i =0 ; i<[rangeString count]; i++) 
    { 
     CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].pointSize, NULL); 

     NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]]; 

     if (whiteRange.location != NSNotFound) 
     { 
      [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange]; 
      [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName 
              value:(__bridge id)ctFont 
              range:whiteRange]; 
     } 
    } 

    CGSize expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap]; 

    CATextLayer *textLayer = [[CATextLayer alloc]init]; 
    textLayer.frame =CGRectMake(0,0, label.frame.size.width,expectedLabelSize.height+4); 
    textLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    textLayer.string=mutableAttributedString; 
    textLayer.opacity = 1.0; 
    textLayer.alignmentMode = kCAAlignmentLeft; 
    [label.layer addSublayer:textLayer]; 
    [textLayer setWrapped:TRUE]; 

    //label.lineBreakMode = UILineBreakModeWordWrap; 

    label.text = @""; 
} 

và sau khi gọi phương pháp này như sau: -

[CommonFunctions setMultiColorAndFontText:string rangeString:[NSArray arrayWithObjects:str1,str2,str3,str4,str5,str6, nil] label:yourLabel font:[NSArray arrayWithObjects:@"Arial",@"Arial",@"Arial",@"Arial",@"Arial",@"Arial"nil] color:[NSArray arrayWithObjects:(UIColor *)[UIColor colorWithRed:(0/255.f) green:(167/255.f) blue:(230/255.f) alpha:1.0f].CGColor,(UIColor *)[UIColor colorWithRed:(189/255.f) green:(189/255.f) blue:(189/255.f) alpha:1.0f].CGColor, color3,color4,color5,color6,nil]]; 

tôi hy vọng nó sẽ hoạt động tốt cho u

`

0

Tôi đã gặp phải vấn đề tương tự khi phát triển Kể từ khi NSAttributeString hoạt động trên ioS6 n ở trên. Thu thập hơn so với các lớp bên thứ ba, Tôi đề nghị bạn tạo UILabel tùy chỉnh hỗ trợ tùy chỉnh nhãn của bạn Tóm lại, tạo nhiều nhãn (từ bằng lời) bằng cách gán màu, đậm, dài. Và nối tất cả nhãn lại với nhau và tạo đối tượng Nhãn mới

Các vấn đề liên quan