Dan*_*iel 5 iphone objective-c nsattributedstring
我有一个使用NSAttributedString的自定义UITableViewCell.我希望它在选择单元格时改变颜色.如何使NSAttributedString与具有highlightTextColor设置的UILabel具有相同的行为?
我试图改变单元格的函数setSelected和setHighlighted的颜色,但似乎它们被调用到很晚(在touchUpInside上而不是touchDown)
提前致谢!
UILabel子类解决方案
@implementation CustomLabelHighlighted
{
NSAttributedString *savedAttributedString;
}
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
if (!highlighted)
{
[super setAttributedText:savedAttributedString];
return;
}
NSMutableAttributedString *highAttributedString = [savedAttributedString mutableCopy];
NSRange range = NSMakeRange(0, highAttributedString.string.length);
[highAttributedString addAttribute:NSForegroundColorAttributeName value:self.highlightedTextColor range:range];
[super setAttributedText:highAttributedString];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
savedAttributedString = attributedText;
}
@end
Run Code Online (Sandbox Code Playgroud)