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

How do I set the color in Attributed button in iOS?

我想在 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];

UIColor 的 RGB 值需要在 0.01.0 的范围内。您创建的颜色将显示为白色。您需要:

[UIColor colorWithRed:38/255.0 green:105/255.0 blue:255/255.0 alpha: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)};