2016-04-21 80 views
0

我想设置一个标题以UIButton中的自定义颜色加下划线,NSForegroundColorAttributeName: [UIColor colorWithRed:38 green:105 blue:255 alpha:1]未应用于我的按钮。如何在iOS中设置Attributed按钮的颜色?

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]; 

回答

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)}; 
+0

你真的应该申报'w'作为'CGFloat',不只是'float'所以它的类型相匹配的是'UIColor'参数。 – rmaddy

2

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

[UIColor colorWithRed:38/255.0 green:105/255.0 blue:255/255.0 alpha:1] 
相关问题