我想设置一个标题,在UIButton
中使用自定义颜色加下划线,NSForegroundColorAttributeName: [UIColor colorWithRed:38 green:105 blue:255 alpha:1]
未在我的按钮中应用。
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:14],NSForegroundColorAttributeName : [UIColor colorWithRed:38 green:105 blue:255 alpha:1], NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
NSMutableAttributedString *mat = [[NSMutableAttributedString alloc] initWithString:linkUrl];
[mat addAttributes:attrDict range:NSMakeRange (0, mat.length)];
[self.weblink setAttributedTitle:mat forState:UIControlStateNormal];
答案 0 :(得分:2)
UIColor
的RGB值必须在0.0
到1.0
的范围内。您正在创建的颜色将显示为白色。你需要:
[UIColor colorWithRed:38/255.0 green:105/255.0 blue:255/255.0 alpha:1]
答案 1 :(得分:1)
仅更改此行
float w = 255.0;
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:14],NSForegroundColorAttributeName : [UIColor colorWithRed:38/w green:105/w blue:255/w alpha:1], NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};