2013-09-26 50 views
1

我想用RED字mat更新名为cardButton的iOS UIButton的标题。无法更改iOS中的按钮标题样式:attributes字符串

这里是我的代码:

NSMutableAttributedString *mat = [card.contents mutableCopy]; 
NSInteger _stringLength=[mat length]; 
UIColor *color; 
color = [UIColor redColor]; 
[mat addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, _stringLength)]; 
[cardButton setAttributedTitle: mat forState:UIControlStateNormal]; 

NSLog返回字垫正确的字。 我有以下错误。我知道内存管理存在问题,但我不知道是什么。

-[__NSCFString addAttribute:value:range:]: unrecognized selector sent to instance 0x7177370 

谢谢!

+0

的NSLog的_stringLength? –

+1

'card.contents'是什么类型?我猜,它不是'NSMutableAttributedString',因此'mat'既不是。 – Matthias

+0

是的!接得好! Thx – Gabriel

回答

4

更换

NSMutableAttributedString *mat = [card.contents mutableCopy]; 

与此:

NSMutableAttributedString *mat = [[NSMutableAttributedString alloc] initWithString:[card.contents mutableCopy]]; 
+0

非常感谢:) – Gabriel