2013-05-08 18 views
5

有一個字符串一樣,我如何顯示不同的顏色和字體類型在一個UILabel

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

我想顯示這個字符串中單個標籤用不同的字體類型和顏色。

即......

「First」字符串應該用粗體顯示。

「Second」字符串應該用斜體表示。

「第三」字符串應爲紅色。

「第四」字符串應爲綠色。

「第五」字符串應該是系統字體的粗斜體。

「第六」字符串應該是宋體粗​​體。

「第七條:」字符串應該是宋體加粗尺寸34

我已經通過一些在計算器的答案了,但我沒有得到這些問題的答案任何清晰..

可以在任何的一個提供了一些指導。

在此先感謝。

解決方案:首先根據您的要求操作NSAttributedString

使用NSAttributedString你可以在iOS 6 UILabelattributedString property

Below ios 6.0使用TTAtributeLabeldisplaying attributedString的任何third party attributedLabel

+0

您不能在單個標籤中執行此操作。你需要7個不同的標籤 – 2013-05-08 11:28:39

+1

使用NSAttributedString你可以在ios6中做到這一點uilabel具有attributesString屬性。 – 2013-05-08 11:30:03

+0

下面的ios 6.0使用TTAtributeLabel來顯示attributesString。 – 2013-05-08 11:31:54

回答

1

在iOS中6.0及以上UILabel有一個屬性:
@property(nonatomic,copy) NSAttributedString *attributedText
接受一個NSAttributedString

您可以創建一個符合您要求的NSAttributedString

+0

什麼是低於iOS 6.0? – 2013-05-08 11:40:04

0

試試吧....

DAAttributedStringUtils

,也看到this

Different Label

希望我幫助

+0

檢查這個http://soulwithmobiletechnology.blogspot.in/2012/07/how-to-use-nsattributedstring-in-ios-6.html – 2013-05-08 11:58:30

+0

@Prince:你說什麼?請詳細解釋 – 2013-05-08 12:07:17

3

使用此代碼設置不同顏色不同的字。 。

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); 

你也可以設置顏色與圖像像波紋管..

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

見我的另一個回答This Link

我希望這對你有幫助...

1

如果u不使用NSAttributeLabel然後 試試這個,它的作品對我來說很好

添加這個方法到CommonFuction類&添加此框架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 = @""; 
} 

,並調用此方法如下之後: -

[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]]; 

我希望它會工作得很好ü

`

0

我在開發時遇到同樣的問題。因爲NSAttributeString在上面的ioS6 n以上工作。而不是獲得第三方類, 我建議您創建自定義UILabel,它支持自定義您的標籤 簡而言之,通過分配顏色,粗體,長度來創建許多標籤(逐字)。 並將所有標籤附加在一起並創建一個新的標籤對象

相關問題