4

I want to set two colors for UILabel's text. I tried TTTRegexAttributedLabel, but it is throwing unknown type error.

I tried following code too. But it is crashing at 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];

Is there any other way to set multiple colors for single UILabel text?

Krunal
  • 77,632
  • 48
  • 245
  • 261
Coder
  • 1,661
  • 4
  • 27
  • 50

3 Answers3

3

you can set text color with pattern image like bellow..

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

and also set different color with this bellow code.. please check tutorial with detail mate..

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

for more information see this tutorial....

coretext-tutorial-for-ios-part

i hope this help you...

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • What are kCTForegroundColorAttributeName etc. I am getting undeclared identifier. – Coder Nov 27 '12 at 07:43
  • hey mate just follow this http://soulwithmobiletechnology.blogspot.in/2011/06/coretext-tutorial-for-ios-part-1.html tutorial and get output which you want mate i'll also post another code for this – Paras Joshi Nov 27 '12 at 07:45
  • Hi mate.. Even in this code, we have kCTForegroundColorAttributeName parameter. Do we need to include any framework for this? – Coder Nov 27 '12 at 07:53
1

NSAttributedString has to be set using UILabel's attributedText property. e.g [syncStatusLabel setAttributedText:str] in your case. Good Luck!

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • And, remember, `NSAttributedString` is available only on iOS6. If you want to support older versions of iOS, you should use other solutions like @waheeda suggested in his(her?) comment. – Fahri Azimov Nov 27 '12 at 07:29
  • This is what i was about to mention. I am using lesser versions of IOS. – Coder Nov 27 '12 at 07:39
0

Try this with swift (execute code with following extension)

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

}

Try an extension with 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)

Here is result:

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261