富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString

//获取富文本
NSMutableAttributedString*attributeString_atts=[[NSMutableAttributedString alloc]initWithString:string];
//背景色
UIColor *backgroundColor=[UIColor whiteColor];
//字体
UIFont *font=[UIFont fontWithName:@"STHeitiSC-Light" size:18.0];
//偏移量
NSNumber *baseLineOffset=[NSNumber numberWithFloat:10.0];
//文本颜色
UIColor *foregroundColor=[UIColor blackColor];
//字间距
NSNumber *kern=[NSNumber numberWithFloat:1.5];
//连体字
NSNumber *ligature=[NSNumber numberWithFloat:];
//超链接文本
NSURL *linkURL=[NSURL URLWithString:@"www.baidu.com"];
//下划线格式
NSNumber *underLine=[NSNumber numberWithInt:NSUnderlineStyleSingle];
//段落格式 段落中有更多段落的格式
NSMutableParagraphStyle *paragraphStyle=[[NSMutableParagraphStyle alloc]init];//新起行缩进
// [paragraphStyle setHeadIndent:10.0];
//首行缩进
[paragraphStyle setFirstLineHeadIndent:21.0];    //行高倍数因子
   [paragraphStyle setLineHeightMultiple:1.3];
   //行间距
[paragraphStyle setLineSpacing:10.0];
   //上述两属性说明:行间距是每行之间距离,计算高度时,可能出现偏差;行高倍数因子,行高固定。
//断行 默认按单词
// paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
//
// [paragraphStyle setTailIndent:375.0];
NSDictionary *attrsDic=@{
NSForegroundColorAttributeName:foregroundColor,
NSBackgroundColorAttributeName:backgroundColor,
// NSBaselineOffsetAttributeName:baseLineOffset,
NSFontAttributeName:font,
NSKernAttributeName:kern,
NSLigatureAttributeName:ligature,
// NSLinkAttributeName:linkURL,
// NSUnderlineStyleAttributeName:underLine,
NSParagraphStyleAttributeName:paragraphStyle
};   //全文使用
[attributeString_atts addAttributes:attrsDic range:NSMakeRange(, [string length])];   //计算富文本的宽度
CGRect attrsRect=[attributeString_atts boundingRectWithSize:CGSizeMake(self.view.frame.size.width-, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
NSLog(@"%f",attrsRect.size.width);
NSLog(@"%f",attrsRect.size.height);
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(, *number, self.view.frame.size.width-, attrsRect.size.height)];
label.attributedText=attributeString_atts;
label.userInteractionEnabled=YES;
label.numberOfLines=;
[self.view addSubview:label];

AttributedString的创建方式,下面研究下AttributedString究竟可以设置哪些属性,具体来说,有以下21个:
 
// NSFontAttributeName                        设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam          设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName                设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName                    设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName          设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName             设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName             填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName                  设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName              设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName               设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName        设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName                      设置链接属性,点击后调用打开指定URL地址
// NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象

paragraph
  alignment             //对齐方式
  firstLineHeadIndent     //首行缩进
  headIndent             //缩进
  tailIndent              //尾部缩进
  lineBreakMode          //断行方式
  maximumLineHeight      //最大行高
  minimumLineHeight      //最低行高
  lineSpacing              //行距
  paragraphSpacing         //段距
  paragraphSpacingBefore  //段首空间
  baseWritingDirection      //句子方向
  lineHeightMultiple      //可变行高,乘因数。
  hyphenationFactor     //连字符属性

ios富文本的简单使用 AttributedString的更多相关文章

  1. iOS富文本组件的实现—DTCoreText源码解析 数据篇

    本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...

  2. UEditor富文本编辑器简单使用

    UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...

  3. iOS - 富文本AttributedString

    最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢?         ...

  4. iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) {         ; i < styles.count; i ++) {      ...

  5. iOS - UILabel添加图片之富文本的简单应用

    //创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...

  6. iOS富文本(一)属性化字符串

    概述 iOS一些复杂的文本布局一般都是由底层的Core Text来实现的,直到iOS7苹果发布了Text Kit框架,Text Kit能够很简单实现一些复杂的文本样式以及布局,而Text Kit富文本 ...

  7. iOS富文本

    背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...

  8. iOS - 富文本

    iOS--NSAttributedString超全属性详解及应用(富文本.图文混排)   ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线.删除线.斜体.空心字体.背景色.阴影以及图文 ...

  9. OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用

    要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...

随机推荐

  1. JS中的onclick事件

    原文链接:https://segmentfault.com/q/1010000007955542?_ea=1503986 我自己做了一下测试. 这个是在html里面直接绑定onclick事件,我打印了 ...

  2. Filebeat issue 排查--single.go:140: ERR Connecting error publishing events (retrying): dial tcp ****:5044: i/o timeout

    我个人用docker搭建了一套日志分析平台:ELK+Filebeat 在正常跑了半个多月之后,Kibana刷新日志时突然发现日志不在更新了,停在某个时刻,就再也没有新log. 首先我查看了elk,lo ...

  3. 使用Visual Studio 2017作为Linux C++开发工具

    Visual Studio 2017 微软的宇宙第一IDE Visual Studio 2017正式版出来了,地址是:https://www.visualstudio.com/vs/whatsnew/ ...

  4. 温故而知新——map

    map  :关联式容器,使用时是以属性值对的方式进行使用,例如:<key,value>.map key值唯一,相同的key值插入时只会保留一个.除此之外,map的特点还包括:1.map底层 ...

  5. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  6. 修改Delphi 10.1.2 edit控件在android的复制、剪切和粘贴样式

    Delphi 10.1.2 edit控件在android默认的复制.剪切和粘贴样式太丑,经悟能-DelphiTeacher的提示,用最简单的代码修改后稍有改观. 默认的样式: 修改后的样式: 修改FM ...

  7. 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  8. 深入浅出数据结构C语言版(3)——递归简论

      相信学习过C语言的读者都已经接触过递归(不论是谭浩强的C程序设计还是C Primer Plus都有递归程序),本文就是对递归的基本原则进行简要介绍.首先,我们写一个基本的递归函数作为例子: int ...

  9. C#全局鼠标键盘Hook

    原文出自:http://www.cnblogs.com/iEgrhn/archive/2008/02/17/1071392.html using System; using System.Collec ...

  10. WinFrom中使用WPF的窗体

    步骤 1.添加WindowsFormsIntegration.dll .System.Windows.Forms.和System.Xaml,PresentationCore.PresentationF ...