如何在iOS中的Attributed按钮中设置颜色?

时间:2016-04-21 13:54:32

标签: ios objective-c nsattributedstring

我想设置一个标题,在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];

2 个答案:

答案 0 :(得分:2)

UIColor的RGB值必须在0.01.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)};