一、在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想。后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现。

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

实例化方法:

使用字符串初始化

- (id)initWithString:(NSString *)str;

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

字典中存放一些属性名和属性值,如:

NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:

[UIFontsystemFontOfSize:15.0],NSFontAttributeName,

[UIColorredColor],NSForegroundColorAttributeName,

NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

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

- (id)initWithAttributedString:(NSAttributedString *)attester;

使用NSAttributedString初始化,跟NSMutableString,NSString类似

使用方法:

为某一范围内文字设置多个属性

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

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

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

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

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

移除某范围内的某个属性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

*常见的属性及说明

NSFontAttributeName  字体

NSParagraphStyleAttributeName  段落格式

NSForegroundColorAttributeName  字体颜色

NSBackgroundColorAttributeName   背景颜色

NSStrikethroughStyleAttributeName 删除线格式

NSUnderlineStyleAttributeName      下划线格式

NSStrokeColorAttributeName        删除线颜色

NSStrokeWidthAttributeName 删除线宽度

NSShadowAttributeName  阴影

更多方法和属性说明详见苹果官方说明文档:

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

使用实例:

//描述设置行距

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[paragraphStyle setLineSpacing:4.0f];

//设置字体

NSDictionary *deitalAttributes = @{NSFontAttributeName :lbl_fetusDesc.font,

NSParagraphStyleAttributeName: paragraphStyle};

NSString *descStr = (self.topViewType == PregnantTopViewTypePregnant) ? _pregInfo.personateFetusAdviceDesc  : _pregInfo.bbchange;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:descStr];

[attributedString addAttributes:deitalAttributes range:NSMakeRange(0, [descStr length])];

二、在上面用到了NSMutableParagraphStyle设置行间距,下面列举一下NSMutableParagraphStyle的用处

//   NSParagraphStyleAttributeName 段落的风格(设置首行,行间距,对齐方式什么的)看自己需要什么属性,写什么
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字体的行间距
paragraphStyle.firstLineHeadIndent = 20.0f;//首行缩进
paragraphStyle.alignment = NSTextAlignmentJustified;//(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.headIndent = 20;//整体缩进(首行除外)
paragraphStyle.tailIndent = 20;//
paragraphStyle.minimumLineHeight = 10;//最低行高
paragraphStyle.maximumLineHeight = 20;//最大行高
paragraphStyle.paragraphSpacing = 15;//段与段之间的间距
paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向(一共➡️三种)
paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = 1;//连字属性 在iOS,唯一支持的值分别为0和1 /*
NSFontAttributeName 字体大小
NSParagraphStyleAttributeName 段落的风格(设置首行,行间距,对齐方式什么的)
NSKernAttributeName 字间距
*/
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle,
NSKernAttributeName:@(10), };
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];

NSMutableAttributedString及NSMutableParagraphStyle的使用的更多相关文章

  1. iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString

    /NSMutableParagraphStyle/NSMutableAttributedString 组合使 NSString * titlestr=@"日产GT-R"; NSMu ...

  2. iOS 学习 - 23 加载本地 txt 文件, NSMutableParagraphStyle 段落格式,缩放动画,字体间距

    思路: 1.new 一个 Empty 后缀为 .txt 文件,内容随笔拷贝一段 2.用 NSString 接收本地文件,再用一个标题拼接字符串 3.创建一个 NSMutableParagraphSty ...

  3. NSMutableAttributedString可变属性字符串的用法

    适用于:当你想对一个字符串中的某几个字符更改颜色,字体... NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5 ...

  4. NSMutableAttributedString(富文本)的简单使用

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  5. NSMutableAttributedString

    开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...

  6. UILabletext去掉乱码 控制颜色 行高 自定义大小 。显示不同的字体颜色、字体大小、行间距、首行缩进、下划线等属性(NSMutableAttributedString)

    text去掉乱码 设置不同颜色 行高 自定义大小 #import <Foundation/Foundation.h> @interface TextsForRow : NSObject @ ...

  7. iOS使用NSMutableAttributedString实现富文本小结

    NSAttributedString NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距).NSAttributedString对象的默 ...

  8. 富文本处理NSMutableAttributedString

    概述 富文本处理在项目中使用率越来越高.比如像颜色改变突出, 大字号突出处理, 下划线处理, 中划线(删除线)处理等等 详细 代码下载:http://www.demodashi.com/demo/10 ...

  9. 快速创建各种类型的NSAttributeString和NSMutableParagraphStyle

      NSDictionary *attributes = @{ NSForegroundColorAttributeName : [ UIColorredColor ], NSFontAttribut ...

随机推荐

  1. linq to xml操作XML(转)

    转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...

  2. 微信小程序配置详解

    在之前已经通过微信公众平台的官方网站https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html,注册好小程序并且登录成功后(这里主 ...

  3. 【python】python调用shell方法

    在python脚本中,有时候需要调用shell获取一下信息,下面介绍两种常用的调用方法. 第一种,os.system() 这个函数获取的是命令的执行状态,比如 >>> import ...

  4. 编写你的第一个django应用程序2

    从1停止的地方开始,我们将设置数据库,创建您的第一个模型,并快速介绍django自动生成的管理站点 数据库设置 现在,打开mysite/settings.py.这是一个普通的python模块,其中模块 ...

  5. JavaMail发送和接收邮件

    一.JavaMail概述:        JavaMail是由Sun定义的一套收发电子邮件的API,不同的厂商可以提供自己的实现类.但它并没有包含在JDK中,而是作为JavaEE的一部分. 厂商所提供 ...

  6. 20145239杜文超 《Java程序设计》第1周学习总结

    20145239<Java程序设计>第1周学习总结 教材学习内容总结 第一周. 通过教材简单的了解了java的历史.因为之前看过视频,所以有一个大致明了的认识. 识记了Java三大平台:J ...

  7. python循环次数的使用

    a=[str(i) for i in range(88888,88912)] b=[str(i) for i in range(77777,77785)] def f(a,b,k=0,m=0): n= ...

  8. POJ 2976 Dropping tests:01分数规划【二分】

    题目链接:http://poj.org/problem?id=2976 题意: 共有n场考试,每场考试你得的分数为a[i],总分为b[i]. 你可以任意去掉k场考试. 问你最大的 100.0 * ( ...

  9. 常见css兼容问题

    链接的虚线框问题 <!-- html --> <a class="noDashedBox" href="#"><img src=& ...

  10. swoole_table

    Memory Swoole提供了7个内存操作的模块,在多进程编程中可以帮助开发者实现一些特殊的需求. swoole_table 是基于共享内存和锁实现的超高性能,并发数据结构