http://stackoverflow.com/questions/11031623/how-can-i-use-attributedtext-in-uilabel

一、设置UILabel的属性attributedText(NSMutableAttributedString)
NSString *testStr = @"测试";
UILabel *testLab = ...(实例对象)
NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];  
[ps setAlignment:NSTextAlignmentCenter];  
NSDictionary *attribs = @{
              NSFontAttributeName: [UIFont fontWithName:@"Microsoft Yahei" size:45],
              NSParagraphStyleAttributeName:ps};  
NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:testStr attributes:attribs];  
testLab.attributedText = attributedText; 
ps: 没有细看这段代码不过查了一下attributedText这个属性可以实现一个label显示多段不同颜色不同字体的文字
二、设置行距、行间距(NSMutableParagraphStyle)

UITextView *lab = [LocalTexts objectAtIndex:j];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
paragraphStyle.minimumLineHeight = 50.0f;
NSString *string = lab.text;
NSDictionary *ats = @{
NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f],
NSParagraphStyleAttributeName : paragraphStyle,
};
lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];//textview 设置行间距
ps:感觉好像上面是设置行距,下面说它还有个属性是lineSpacing,效果不佳试试这个

三、设置对齐方式、折行方式
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;//设置对齐方式
paragraph.lineBreakMode = NSLineBreakByWordWrapping;

四、NSMutableParagraphStyle、NSAttributedString(简单介绍,详细看API吧)

NSAttributedString的初始化方法有

-initWithString:用String初始化,并没有Attributed信息。

-initWithAttributedString:用AttributedString去初始化。

-initWithString:Attributed:用string及attribute的dictionary来初始化。

具体AttributtedString属性的键值对如下:
NSString *const NSFontAttributeName;//值为UIFont,设置字体,默认值为12-point Helvetica(Neue) 。

下面这段代码可以查看ios中可用的字体,具体那些字体长什么样,可以查看字体册工具。

NSArray *familyArray = [UIFont familyNames];
for (id family in familyArray)
{
  printf(“%s\n”,[family cStringUsingEncoding:NSUTF8StringEncoding]);
  NSArray *fontArray = [UIFont fontNamesForFamilyName:family];
  for (id font in fontArray)
  {
   printf(”   %s\n”,[font cStringUsingEncoding:NSUTF8StringEncoding]);
  }
}
NSString *const NSParagraphStyleAttributeName;//值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。

NSMutableParagraphStyle与NSParagraphStyle包括一下属性
  alignment //对齐方式
  firstLineHeadIndent //首行缩进
  headIndent //缩进
  tailIndent  //尾部缩进
  lineBreakMode  //断行方式
  maximumLineHeight  //最大行高
  minimumLineHeight  //最低行高
  lineSpacing  //行距
  paragraphSpacing  //段距
  paragraphSpacingBefore  //段首空间
  baseWritingDirection  //句子方向
  lineHeightMultiple  //可变行高,乘因数。
  hyphenationFactor //连字符属性
NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色,默认为黑色。
NSString *const NSBackgroundColorAttributeName;//值为UIColor,字体背景色,默认没有。
NSString *const NSLigatureAttributeName;//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。
NSString *const NSKernAttributeName;//值为浮点数NSNumber,字距属性,默认值为0。
NSString *const NSStrikethroughStyleAttributeName;//值为整型NSNumber,可取值为
enum {
   NSUnderlineStyleNone = 0×00,
   NSUnderlineStyleSingle = 0×01,
};设置删除线。
NSString *const NSUnderlineStyleAttributeName;//同上。设置下划线。
NSString *const NSStrokeColorAttributeName;//值为UIColor,默认值为nil,设置的属性ForegroundColor。
NSString *const NSStrokeWidthAttributeName;//值为浮点数NSNumber。设置比画的粗细。
NSString *const NSShadowAttributeName;//值为NSShadow,设置比画的阴影,默认值为nil。
NSString *const NSVerticalGlyphFormAttributeName;//值为整型NSNumber,0为水平排版的字,1为垂直排版的

转载于:https://www.cnblogs.com/ymlike7/p/4629535.html

【转】iphone开发中NSMutableAttributedString/NSAttributedString 富文本设置相关推荐

  1. 安卓开发中SpannableString之富文本显示效果

    https://www.cnblogs.com/qynprime/p/8026672.html 转载于:https://www.cnblogs.com/adressian/p/10290564.htm ...

  2. iPhone开发中的技巧整理(四)

    iphone开发笔记 退回输入键盘 - (BOOL) textFieldShouldReturn:(id)textField{ [textField  resignFirstResponder]; } ...

  3. Qt开发技术:Qt富文本(三)Qt支持的HTML子集(查询手册)以及涉及的类

    若该文为原创文章,未经允许不得转载 原博主博客地址:https://blog.csdn.net/qq21497936 原博主博客导航:https://blog.csdn.net/qq21497936/ ...

  4. Qt开发技术:Qt富文本(一)富文本介绍、文档结构

    若该文为原创文章,未经允许不得转载 原博主博客地址:https://blog.csdn.net/qq21497936 原博主博客导航:https://blog.csdn.net/qq21497936/ ...

  5. iPhone开发中现文件的增加 删除和查询

    iPhone开发中,我们常常用到一些对于文件的增加,删除和查询,这些基本的功能对于开发者来说非常的重要,而且非常实用.本文给大家介绍一下如何实现这几个具体的功能. //创建文件 -(void)Crea ...

  6. iphone iPhone开发中为UINavigationBar设置背景图片方法

    1:原文摘自:http://mobile.51cto.com/iphone-284865.htm iPhone开发中为UINavigationBar设置背景图片方法是本文要介绍的内容,在iPhone开 ...

  7. 学习iPhone开发中 sqlite3的使用

    由于我主要负责我们小组项目数据库模块的部分所以这几天都一直在研究在iphone中最为常用的一个简单数据库sqlite,自己也搜集很多资料,因此在这里总结一下这几天的学习成果: 1.Sqlite操作简明 ...

  8. iphone开发中图像处理相关要点

    iPhone图像通常存储在以下4个地方: 相册(PhotoAlums):用户可以使用UIImagePickerController类提供的交互对话框从该相册中获取图像. 应用程序包:将图像与可执行程序 ...

  9. iphone iPhone开发中如何将制作图片放大缩小代码实现案例

    1:原文摘自:http://mobile.51cto.com/iphone-285108.htm iPhone开发中如何将制作图片放大缩小案例是本文要介绍的内容,主要是来学习iphone开发中动画的制 ...

最新文章

  1. mysql表和表的关系_mysql表与表之间建关系
  2. java继承的知识点_Java知识点梳理——继承
  3. Leetcode 105. 从前序与中序遍历序列构造二叉树 解题思路及C++实现
  4. linux投屏快捷键,Linux基本指令(持续更新中..)
  5. js时间工具 MyTimeUtil.js
  6. 安装centos7失败认不到硬盘_CentOS7 用U盘安装卡住无法进入安装界面解决方案
  7. MongoDB 入门,我是花了心思的
  8. 没事研究下C#虚拟光驱,有所收获!
  9. Linux系统安装管理
  10. php访问属性两种方式,使用PHP访问对象的属性
  11. 命令提示符cmd以管理员身份运行的快捷键
  12. 如何使用Pandas进行数据分析!最详细的数据分析教程!
  13. 计算机英语versatile意思,英语单词versatile是什么意思,英文单词查询versatile,在线单词versatile翻译...
  14. 量化策略篇:股票多头策略、CTA策略、期权策略
  15. 【转载】关于MSHTML
  16. 基于ROS的qbo机器人
  17. 数据库应用(mysql)数据库管理
  18. 【科创人】联软科技张建耀:不擅长营销的拓荒高手,企业长期发展必经管理变革
  19. 计算机专业大创要求,“大创项目”推动计算机专业学生创新实践能力的提高
  20. 为什么INC,DEC指令不影响CF标志位呢?

热门文章

  1. [Hadoop]SSH免密码登录以及失败解决方案
  2. bat命令运行java程序
  3. informix数据库 java 增删改查
  4. perl如何遍历指定文件夹下的指定扩展名文件,并按时间顺序要求删除
  5. ES5-String-indexOf/lastIndexOf
  6. 新站结合熊掌号的实际操作 实现当天收录
  7. 东芝出售西屋电气在即
  8. Docker学习笔记 - Docker容器的日志
  9. 【转】移动客户端测试总结
  10. 如何写代码,才能越写越轻松?