今天就跟大家聊聊有关怎么在iOS中使用NSMutableAttributedString实现富文本,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联公司是专业的伊春网站建设公司,伊春接单;提供做网站、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行伊春网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
NSAttributedString
NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距)。NSAttributedString对象的默认字体是Helvetica 12点,可能与平台的默认系统字体不同。因此,您可能希望创建适用于您的应用程序的非默认属性的新字符串。您还可以使用NSParagraphStyle类及其子类NSMutableParagraphStyle来封装NSAttributedString类使用的段落或标尺属性。
实例化方法和使用方法
实例化方法
使用字符串初始化
- (instancetype)initWithString:(NSString *)str;
代码示例
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据"];
字典中存放一些属性名和属性值
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary*)attrs;
代码示例
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据" attributes:attributedDict];使用NSAttributedString初始化,与NSMutableString,NSString类似
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
使用方法
为某一范围内的文字设置多个属性的方法
- (void)setAttributes:(NSDictionary*)attrs range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)];为某一范围内的文字添加某个属性的方法
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//代码示例
NSString *string = @"测试数据"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];
为某一范围内的文字添加多个属性的方法
- (void)addAttributes:(NSDictionary*)attrs range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];移除某个范围内的某个属性的方法
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];
[attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)];属性及说明
| key | 说明 |
|---|---|
| NSFontAttributeName | 字体,value是UIFont对象 |
| NSParagraphStyleAttributeName | 绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象 |
| NSForegroundColorAttributeName | 文字颜色,value是UIFont对象 |
| NSLigatureAttributeName | 字符连体,value是NSNumber |
| NSKernAttributeName | 字符间隔 |
| NSStrikethroughStyleAttributeName | 删除线,value是NSNumber |
| NSUnderlineStyleAttributeName | 下划线,value是NSNumber |
| NSStrokeColorAttributeName | 描绘边颜色,value是UIColor |
| NSStrokeWidthAttributeName | 描边宽度,value是NSNumber |
| NSShadowAttributeName | 阴影,value是NSShadow对象 |
| NSTextEffectAttributeName | 文字效果,value是NSString |
| NSAttachmentAttributeName | 附属,value是NSTextAttachment 对象 |
| NSLinkAttributeName | 链接,value是NSURL or NSString |
| NSBaselineOffsetAttributeName | 基础偏移量,value是NSNumber对象 |
| NSStrikethroughColorAttributeName | 删除线颜色,value是UIColor |
| NSObliquenessAttributeName | 字体倾斜 |
| NSExpansionAttributeName | 字体扁平化 |
| NSVerticalGlyphFormAttributeName | 垂直或者水平,value是 NSNumber,0表示水平,1垂直 |
富文本段落排版格式属性说明
| 属性 | 说明 |
|---|---|
| lineSpacing | 字体的行间距 |
| firstLineHeadIndent | 首行缩进 |
| alignment | (两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然) |
| lineBreakMode | 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") |
| headIndent | 整体缩进(首行除外) |
| minimumLineHeight | 最低行高 |
| maximumLineHeight | 最大行高 |
| paragraphSpacing | 段与段之间的间距 |
| paragraphSpacingBefore | 段首行空白空间 |
| baseWritingDirection | 书写方向(一共三种) |
| hyphenationFactor | 连字属性 在iOS,唯一支持的值分别为0和1 |
看完上述内容,你们对怎么在iOS中使用NSMutableAttributedString实现富文本有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
文章标题:怎么在iOS中使用NSMutableAttributedString实现富文本
URL链接:http://www.jxjierui.cn/article/ijjhog.html


咨询
建站咨询
