[英]Button title color
I want to set button title with underline and of browncolor: 我想设置带下划线和褐色的按钮标题:
For that I am doing: 为此我正在做:
NSMutableAttributedString *nearestLocation = [[NSMutableAttributedString alloc] initWithString:@"Locate Manually"];
[nearestLocation addAttribute:(NSString*)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:(NSRange){0,[nearestLocation length]}];
[btn_manually setAttributedTitle:nearestLocation forState:UIControlStateNormal];
[btn_manually setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
It is displaying underline but not browncolor. 它显示下划线但不是棕色。
尝试使用这样....
[nearestLocation addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(0,[nearestLocation length])];
In iOS, NSAttributedString is used modifying the text, you can use "NSMutableAttributedString" for multi color text, font, style, etc using single UIButton or UILabel. 在iOS中,NSAttributedString用于修改文本,您可以使用单个UIButton或UILabel将“NSMutableAttributedString”用于多色文本,字体,样式等。
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"The Underlined text"];
// making text property to underline text-
[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];
// using text on button
[btn_manually setAttributedTitle: titleString forState:UIControlStateNormal];
btn_manually.titleLabel.textColor = [UIColor brownColor];
NSString *tempString=@"your string";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:tempString];
int lenght=(int)[tempString length];
[attString addAttribute:(id)NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(0,lenght)];
[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, length)];
UIButton *hyperLinkButton=[UIButton buttonWithType:UIButtonTypeCustom];
hyperLinkButton.frame=CGRectMake(30, 5, 100, 30);
[hyperLinkButton setAttributedTitle:attString forState:UIControlStateNormal];
hyperLinkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
hyperLinkButton.tag=[[[[[[personalDetailDictionary objectForKey:@"Acknowledgements"] objectForKey:@"details"] objectAtIndex:i] objectForKey:@"tagValue"] objectForKey:@"text"] intValue];
[hyperLinkButton addTarget:self action:@selector(hyperLinkAction:) forControlEvents:UIControlEventTouchUpInside];
hyperLinkButton.titleLabel.textAlignment=NSTextAlignmentLeft;
[self.view addSubview:hyperLinkButton];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.