场景:
1. 自定义view或其他控件时, 往往需要根据背景色或图片绘制特定颜色的文本.
NSMutableDictionary* stringAttributes = [NSMutableDictionary dictionaryWithCapacity:1]; //[stringAttributes setObject:[NSFont messageFontOfSize:12.0] forKey:NSFontAttributeName]; [stringAttributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName]; NSString* systemCapacity = [NSString stringWithFormat:@"System: %@",[FileUtil ZLGetFormatSizeFromBytes:system]]; NSSize systemSize = [systemCapacity sizeWithAttributes:stringAttributes]; theRect1.origin.x += (theRect1.size.width - systemSize.width)/2; [systemCapacity drawAtPoint:theRect1.origin withAttributes:stringAttributes];
说明:
主要时添加了绘制属性, 具体有哪些属性看头文件 NSAttributedString.h
以下是部分属性:
/* Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below. */ APPKIT_EXTERN NSString *NSFontAttributeName; // NSFont, default Helvetica 12 APPKIT_EXTERN NSString *NSParagraphStyleAttributeName; // NSParagraphStyle, default defaultParagraphStyle APPKIT_EXTERN NSString *NSForegroundColorAttributeName; // NSColor, default blackColor APPKIT_EXTERN NSString *NSUnderlineStyleAttributeName; // NSNumber containing integer, default 0: no underline APPKIT_EXTERN NSString *NSSuperscriptAttributeName; // NSNumber containing integer, default 0 APPKIT_EXTERN NSString *NSBackgroundColorAttributeName; // NSColor, default nil: no background APPKIT_EXTERN NSString *NSAttachmentAttributeName; // NSTextAttachment, default nil APPKIT_EXTERN NSString *NSLigatureAttributeName; // NSNumber containing integer, default 1: default ligatures, 0: no ligatures, 2: all ligatures APPKIT_EXTERN NSString *NSBaselineOffsetAttributeName; // NSNumber containing floating point value, in points; offset from baseline, default 0 APPKIT_EXTERN NSString *NSKernAttributeName; // NSNumber containing floating point value, in points; amount to modify default kerning, if 0, kerning off APPKIT_EXTERN NSString *NSLinkAttributeName; // NSURL (preferred) or NSString APPKIT_EXTERN NSString *NSStrokeWidthAttributeName; // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0) APPKIT_EXTERN NSString *NSStrokeColorAttributeName; // NSColor, default nil: same as foreground color APPKIT_EXTERN NSString *NSUnderlineColorAttributeName; // NSColor, default nil: same as foreground color APPKIT_EXTERN NSString *NSStrikethroughStyleAttributeName; // NSNumber containing integer, default 0: no strikethrough APPKIT_EXTERN NSString *NSStrikethroughColorAttributeName; // NSColor, default nil: same as foreground color APPKIT_EXTERN NSString *NSShadowAttributeName; // NSShadow, default nil: no shadow APPKIT_EXTERN NSString *NSObliquenessAttributeName; // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew APPKIT_EXTERN NSString *NSExpansionAttributeName; // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion APPKIT_EXTERN NSString *NSCursorAttributeName; // NSCursor, default IBeamCursor APPKIT_EXTERN NSString *NSToolTipAttributeName; // NSString, default nil: no