NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。
创建AttributedString演示样例:
UIFont *font = [UIFont fontWithName:@"Courier" size:18.0];
NSDictionary *attriDict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attriStr = [[NSAttributedString alloc] initWithString:@"my attributed string" attributes:attriDict];
self.label.attributedText = attriStr;
一些常见属性:
字体
UIKIT_EXTERN NSString *const NSFontAttributeName NS_AVAILABLE_IOS(6_0); // UIFont, default Helvetica(Neue) 12
文字颜色, 前景色
UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default blackColor
// 背景色
UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: no background
// strike-through line 中划线
UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no strikethrough
// strike-though line 颜色
UIKIT_EXTERN NSString *const NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color
// underline-style 下划线
UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no underline
// 下划线颜色
UIKIT_EXTERN NSString *const NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color
// 字体粗度
UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0); // 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)
// 模糊度
UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(6_0); // NSShadow, default nil: no shadow
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 5; // 模糊度
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 3);
NSShadowAttributeName:shadow
倾斜
UIKIT_EXTERN NSString *const NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
创建AttributedString演示样例:
UIFont *font = [UIFont fontWithName:@"Courier" size:18.0];
NSDictionary *attriDict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attriStr = [[NSAttributedString alloc] initWithString:@"my attributed string" attributes:attriDict];
self.label.attributedText = attriStr;
一些常见属性:
字体
UIKIT_EXTERN NSString *const NSFontAttributeName NS_AVAILABLE_IOS(6_0); // UIFont, default Helvetica(Neue) 12
文字颜色, 前景色
UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default blackColor
// 背景色
UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: no background
// strike-through line 中划线
UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no strikethrough
// strike-though line 颜色
UIKIT_EXTERN NSString *const NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color
// underline-style 下划线
UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no underline
// 下划线颜色
UIKIT_EXTERN NSString *const NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color
// 字体粗度
UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0); // 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)
// 模糊度
UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(6_0); // NSShadow, default nil: no shadow
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 5; // 模糊度
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 3);
NSShadowAttributeName:shadow
倾斜
UIKIT_EXTERN NSString *const NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew