0

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.

Krunal
  • 77,632
  • 48
  • 245
  • 261
Senthil
  • 510
  • 12
  • 28

6 Answers6

4

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 !!!

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
1

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;
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • OP stated that he is developing for iOS 4.3 and onwards. attributedText property won't work for iOS 4 and 5. – Bartu May 22 '13 at 13:06
0

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.

Bartu
  • 2,189
  • 2
  • 26
  • 50
0

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)
Krunal
  • 77,632
  • 48
  • 245
  • 261
0

Try This:

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

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

Marcin Kuptel
  • 2,674
  • 1
  • 17
  • 22