给NSAttributedString添加属性
text.stringValue和attributedStringValue的关系
通过dict添加属性
NSTextField *text = [NSTextField alloc]init];
NSMutableDictionary *textAttributes = [NSMutableDictionary dictionary];
NSFont *font = [NSFont fontWithName:@"Arial" size:30.0];
textAttributes[NSFontAttributeName] = font;
NSNumber *strokeWidth = @-2.0;
textAttributes[NSStrokeColorAttributeName] = [NSColor redColor];
textAttributes[NSStrokeWidthAttributeName] = strokeWidth;
textAttributes[NSTextEffectAttributeName] = NSTextEffectLetterpressStyle;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text.stringValue attributes:textAttributes];
text.attributedStringValue = attributedText;
直接添加
NSTextField *text = [NSTextField alloc]init];
NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"你好"] autorelease];
[attributedString addAttribute:NSStrokeWidthAttributeName value:@-2.0 range:NSMakeRange(0, attributedString.length)];
[text setAttributedStringValue:attributedString];
修改font字体
正常更改字体的方法
textField setFont:[NSFont systemFontOfSize:16]
NSFont *font = [NSFont fontWithName:@"Arial" size:size];
通过字符串指定字体类型,并可以选择size 和 weight
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *font = [fontManager fontWithFamily:@"Helvetica Neue" traits:NSFontBoldTrait weight:0.4 size:32];
[attributedString addAttributes:@{NSFontAttributeName: font} range:NSMakeRange(0, attributedString.length)];
通过NSColorSpace获取颜色
CGFloat red, green, blue, alpha;
NSColorSpace *colorSpace = [NSColorSpace genericRGBColorSpace];
NSColor* textColor = [text.textColor colorUsingColorSpace:colorSpace];
[textColor getRed:&red green:&green blue:&blue alpha:&alpha];