IOS中使用NSAttributedString灵活创建标签
来源:易贤网 阅读:1012 次 日期:2015-05-08 14:30:55
温馨提示:易贤网小编为您整理了“IOS中使用NSAttributedString灵活创建标签”,方便广大网友查阅!

灵活使用NSAttributedString可以更轻松的创建出内容复杂的标签。需要注意一点:如果一个label设置了这个属性,那它其他的设置都将失效。

首先,我们初始化一个NSMutableAttributedString对象。

//通过字符串初始化

//- (instancetype)initWithString:(NSString *)str;

//通过字符串和属性字典直接初始化

//- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

//通过自身对象初始化

//- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc]initWithString:@"123!@#你好么QWE"];

可以通过下面两个函数对attrebute字符串进行设置与修改

//可以替换字符

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;

//属性设置

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

//设置一定范围内字符属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

字典的键值对应如下:

//kCTFontAttributeName 这个键是字体的名称 必须传入CTFont对象

//kCTKernAttributeName 这个键设置字体间距 传入必须是数字对象 默认为0

//kCTLigatureAttributeName 这个键设置连字方式 必须传入CFNumber对象

//kCTParagraphStyleAttributeName 段落对其方式

//kCTForegroundColorAttributeName 字体颜色 必须传入CGColor对象

//kCTStrokeWidthAttributeName 笔画宽度 必须是CFNumber对象

//kCTStrokeColorAttributeName 笔画颜色

//kCTSuperscriptAttributeName 控制垂直文本定位 CFNumber对象

//kCTUnderlineColorAttributeName 下划线颜色

[attribute addAttribute:(NSString*)kCTKernAttributeName value:@5 range:NSMakeRange(0, 5)];

[attribute addAttribute:(NSString *)kCTFontAttributeName

value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,

14,

NULL))

range:NSMakeRange(0, 4)];

[attribute addAttribute:(NSString *)kCTUnderlineStyleAttributeName

value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]

range:NSMakeRange(0, 4)];

通过测试,发现上面有些键值并没有作用,可以替换下面的方法,效果相同,不同的地方在于其传值的类型不同,下面的方法更加方便(使用UIFont UIColor NSString 和一些系统枚举)

NSParagraphStyleAttributeName

NSForegroundColorAttributeName

NSBackgroundColorAttributeName

NSLigatureAttributeName

NSKernAttributeName

NSStrikethroughStyleAttributeName

NSUnderlineStyleAttributeName

NSStrokeColorAttributeName

NSStrokeWidthAttributeName

NSShadowAttributeName

NSTextEffectAttributeName

NSAttachmentAttributeName

NSLinkAttributeName

NSBaselineOffsetAttributeName

NSUnderlineColorAttributeName

NSStrikethroughColorAttributeName

NSObliquenessAttributeName

NSExpansionAttributeName

NSWritingDirectionAttributeName

NSVerticalGlyphFormAttributeName

更多信息请查看IT技术专栏

更多信息请查看技术文章
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标