2016-04-21 83 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] 
相關問題