【问题标题】:Changing the color of a buttons text in iOS (objective-c)在 iOS 中更改按钮文本的颜色(objective-c)
【发布时间】:2015-11-19 20:31:01
【问题描述】:
我想在加载时将按钮中的文本颜色更改为蓝色。我还只需要按钮文本的前 9 个字母(用户名的长度)为蓝色,而不是所有文本。我该怎么做?
示例:
上图显示“@LisaLisa 正在关注你”。这是按钮文本。如何让“@LisaLisa”变成蓝色,而“正在关注你”保持黑色?
【问题讨论】:
标签:
ios
objective-c
button
colors
【解决方案1】:
UIButton *someButton = [UIButton buttonWithType:UIButtonTypeSystem];
NSString *someUsername = @"@LisaLisa";
NSString *buttonText = [NSString stringWithFormat:@"%@ is following you.", someUsername];
NSRange rangeToHighlight = [buttonText rangeOfString:someUsername];
NSDictionary *defaultAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]};
NSDictionary *highlightedAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor]};
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:buttonText attributes:defaultAttributes];
[attributedTitle addAttributes:highlightedAttributes range:rangeToHighlight];
[someButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
【解决方案2】:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIColor *color1 = [UIColor redColor];
UIColor *color2 = [UIColor blueColor];
UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue" size:20.0f];
UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = @{NSFontAttributeName:font1,
NSForegroundColorAttributeName:color1 }; // Added line
NSDictionary *dict2 = @{NSFontAttributeName:font2,
NSForegroundColorAttributeName:color2 }; // Added line
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:senderName attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"posted to" attributes:dict2]];
[cell.profileIDButton setAttributedTitle:attString forState:UIControlStateNormal];
[[cell.profileIDButton titleLabel] setNumberOfLines:0];
[[cell.profileIDButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
这个答案对我有用。我还不能测试 Andre 的答案。
【解决方案3】:
UILabel * myLabel = [[UILabel alloc] init];//used for whole string
myLabel.numberOfLines = 0;
NSString * myUserName = @"@LisaLisa";//used for userName
//add button on UserName
UIButton * muButton = [[UIButton alloc] init];
myLabel.text = [NSString stringWithFormat:@"%@", myUserName];
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];
//Here width is your Label's Width. Because we have to make sure that our, our label's width is not greater than our device's width and fontSize is label's fontSize
muButton.frame = myLabel.frame;
myLabel.text = [NSString stringWithFormat:@"%@ is following you", myUserName];
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];//used for making dynamic height of label
//For changing color of UserName
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: myLabel.attributedText];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, myUserName.length)];
[myLabel setAttributedText: text];
以下方法用于制作标签的动态高度和宽度。因为我们只需要在 UserName ("LisaLisa") 上设置 Button 的框架。
//for setting the dynamic height of labels
-(UILabel *)setDynamicLableFrame:(UILabel*)myLabel fontSize:(float)size Width:(float)Width
{
CGSize possibleSize = [myLabel.text sizeWithFont:[UIFont fontWithName:REGULER_FONT size:size] constrainedToSize:CGSizeMake(300, 9999) lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = myLabel.frame;
newFrame.size.height = possibleSize.height;
if (possibleSize.width < Width) {
newFrame.size.width = possibleSize.width;
}else{
newFrame.size.width = Width;
}
myLabel.frame = newFrame;
return myLabel;
}
希望,这就是您要找的。有任何问题可以联系我。