首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

设立buuton等View的字体颜色

2013-03-27 
设置buuton等View的字体颜色#import Cocoa/Cocoa.h@interface NSButton (TextColor)- (NSColor *)textCo

设置buuton等View的字体颜色

#import <Cocoa/Cocoa.h>@interface NSButton (TextColor)- (NSColor *)textColor;- (void)setTextColor:(NSColor *)textColor;@end#import "NSButton+TextColor.h"@implementation NSButton (TextColor)- (NSColor *)textColor{    NSAttributedString *attrTitle = [self attributedTitle];    int len = [attrTitle length];    NSRange range = NSMakeRange(0, MIN(len, 1)); // take color from first char    NSDictionary *attrs = [attrTitle fontAttributesInRange:range];    NSColor *textColor = [NSColor controlTextColor];    if (attrs) {        textColor = [attrs objectForKey:NSForegroundColorAttributeName];    }    return textColor;}- (void)setTextColor:(NSColor *)textColor{    NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]                                  initWithAttributedString:[self attributedTitle]];    int len = [attrTitle length];    NSRange range = NSMakeRange(0, len);    [attrTitle addAttribute:NSForegroundColorAttributeName                       value:textColor                       range:range];    [attrTitle fixAttributesInRange:range];    [self setAttributedTitle:attrTitle];    [attrTitle release];}@end

?

热点排行