output should be like this
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.
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 !!!
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;
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
.
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)
Try This:
NSMutableAttributedString *newStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Name *"]];
[newStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)];
label.attributedText = newStr;
您应该使用NSAttributedString能够更改单个字符的属性。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.