iOS基础·OC语法篇

iOS-NSAttributedString显示字符串

2016-08-24  本文已影响43人  亦晴工作室

标签内显示不同格式文本(不同字体, 不同颜色, 删除线,下划线)
NSAttributedString显示字符串

  1. 实例化方法和使用方法
    

实例化方法:
使用字符串初始化

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀"];
NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:
                                    [UIFontsystemFontOfSize:15.0],NSFontAttributeName,
                                    [UIColorredColor],NSForegroundColorAttributeName,
                                   NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];

使用方法:
为某一范围内文字设置多个属性

为某一范围内文字添加多个属性

  1. 常见的属性及说明
    

NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName 删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName 删除线宽度
NSShadowAttributeName 阴影

上一篇下一篇

猜你喜欢

热点阅读