Senthil
Senthil

Reputation: 510

UILabel Text Color Change

enter image description here

output should be like this

enter image description here

UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(5,10,300,20)];
name.text =[Language localizedStringForKey:@"Name *"];
name.font = [UIFont systemFontOfSize:14.0f];
[name setBackgroundColor:[UIColor clearColor]];
[self.View addSubview:name];

How to change the color of one letter? I only need label text change.

Upvotes: 0

Views: 3227

Answers (6)

Nick
Nick

Reputation: 875

Try This:

NSMutableAttributedString *newStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Name *"]];
[newStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)];
label.attributedText = newStr;

Upvotes: 0

Krunal
Krunal

Reputation: 79776

Swift 4
(Note: notation for attributed string key is changed in swift 4)

Here is an extension for NSMutableAttributedString, that add/set color on string/text.

extension NSMutableAttributedString {

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

}

Now, try above extension with UILabel and see result

let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
let title = "Name"
let star = "*"
let stringValue = "\(title) \(star)"
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColor(color: UIColor.red, forText: star)   // or use direct value for text "red"
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)

Upvotes: 0

Bhavin
Bhavin

Reputation: 27235

Answer : NSAttributedString

Check this Answer : Answer for iOS5 and iOS6 also.

For Eg :

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Name  *"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5,1)];

Update :

Make this change

[string addAttribute:(NSString*)kCTForegroundColorAttributeName 
                value:(id)[[UIColor redColor] CGColor]
                range:NSMakeRange(5,1)];

Add following after #import line to your .m file :

#if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
#import <CoreText/CoreText.h>
#else
#import <AppKit/AppKit.h>
#endif

GoodLuck !!!

Upvotes: 4

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Try to use this one.This will work on IOS6 or later version.

UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(5,10,300,20)];
name.font = [UIFont systemFontOfSize:14.0f];
[name setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:name];

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Name *"]];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)];
name.attributedText = attrStr;

Upvotes: 1

Bartu
Bartu

Reputation: 2210

Using NSAttributedString in default UILabel is available after iOS 6.0.

In order to support below iOS 6.0, you need to use TTTAttributedLabel. You can find usage and installation instructions on the readme file. Also note that it also uses NSAttributedString. So you can combine TTTAttributedLabel + UILabel to support both iOS 6+ and below.

Of course you can also create your own subclass of UILabel.

Upvotes: 0

Marcin Kuptel
Marcin Kuptel

Reputation: 2664

You should use NSAttributedString to be able to change attributes of single characters.

Upvotes: -1

Related Questions