I am new in iOS. I want to know how can I change the color for each part of UIButton
title.
For example, a part of title in my UIButton
is black and another the part of title in my UIButton
is yellow
Any help would be appreciated
I am new in iOS. I want to know how can I change the color for each part of UIButton
title.
For example, a part of title in my UIButton
is black and another the part of title in my UIButton
is yellow
Any help would be appreciated
U can use NSMutableAttributedString
then add it to button with [button setAttributedTitle:attString forState:UIControlStateNormal];
To change color of a range in the Attribute String, read this, basically its just 1 of the attribute dictionary
Read this SO Question
I solve my question by using the code below.
For UILabel
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.lblFriendBand.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(1, 2)];
[self.lblFriendBand setAttributedText: text];
For UIButton
NSMutableAttributedString *text2 =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.btnFriendName.titleLabel.attributedText];
[text2 addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(1, 2)];
[self.btnFriendName setAttributedTitle:text2 forState:UIControlStateNormal];