2012-11-27 137 views
4

我想爲UILabel's文本設置兩種顏色。我試過TTTRegexAttributedLabel,但它是拋出未知類型的錯誤。UILabel TEXT的兩種顏色

我也試過下面的代碼。但它在settext中崩潰。

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."]; 
    [str addAttribute: @"Hello" value:[UIColor yellowColor] range:NSMakeRange(3,5)]; 
    [str addAttribute:@"That" value:[UIColor greenColor] range:NSMakeRange(10,7)]; 
    [str addAttribute:@"Hello" value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)]; 

[syncStatusLabel setText:(NSString *)str]; 

是否有任何其他方式來設置單個UILabel文本的多種顏色?

+0

https://github.com/AliSoftware/OHAttributedLabel – waheeda

+0

你可以使用這個討論http://stackoverflow.com/questions/3586871/bold-non-bold-text-in-a-single-uilabel – spider1983

+0

我使用較低版本的IOS ..所以setAttribute函數不存在。 .. – Coder

回答

3

您可以設置與像波紋圖案像文本顏色..

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

,還可以設置不同的顏色與此婁代碼..請與隊友詳細教程..

NSString *test = @"Hello. That is a test attributed string."; 

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

爲更多信息見本教程....

coretext-tutorial-for-ios-part

我希望這可以幫助您...

+0

什麼是kCTForegroundColorAttributeName等我得到未申報的標識符。 – Coder

+0

hey mate只要按照這個http://soulwithmobiletechnology.blogspot.in/2011/06/coretext-tutorial-for-ios-part-1.html教程,並得到你想要的輸出,我也會發布另一個代碼 –

+0

喂隊友..即使在這個代碼中,我們有kCTForegroundColorAttributeName參數。我們是否需要爲此包含任何框架? – Coder

1

NSAttributedString必須使用UILabelattributedText屬性進行設置。例如[syncStatusLabel setAttributedText:str]你的情況。祝你好運!

+0

而且,請記住,'NSAttributedString'僅在iOS6上可用。如果你想支持iOS的老版本,你應該使用其他解決方案,比如@waheeda在他的評論中提出的建議。 –

+0

這就是我所要提到的。我正在使用較小版本的IOS。 – Coder

0

與SWIFT(執行代碼以下擴展名)試試這個

extension NSMutableAttributedString { 

    func setColorForText(textToFind: String, withColor color: UIColor) { 
     let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive) 
     self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range) 
    } 

} 

嘗試使用的UILabel的擴展:

self.view.backgroundColor = UIColor.blue.withAlphaComponent(0.5) 
let label = UILabel() 
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200) 

let stringValue = "Hello. That is a test attributed string." // or direct assign single string value like "firstsecondthird" 
label.textColor = UIColor.lightGray 
label.numberOfLines = 0 
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue) 
attributedString.setColorForText(textToFind: "Hello", withColor: UIColor.yellow) 
attributedString.setColorForText(textToFind: "That", withColor: UIColor.green) 

label.font = UIFont.systemFont(ofSize: 26) 
label.attributedText = attributedString 
self.view.addSubview(label) 

下面是結果:

enter image description here