NSMutableAttributedString及NSMutableParagraphStyle的使用
一、在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 阴影
更多方法和属性说明详见苹果官方说明文档:
使用实例:
//描述设置行距
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的使用的更多相关文章
- iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString
/NSMutableParagraphStyle/NSMutableAttributedString 组合使 NSString * titlestr=@"日产GT-R"; NSMu ...
- iOS 学习 - 23 加载本地 txt 文件, NSMutableParagraphStyle 段落格式,缩放动画,字体间距
思路: 1.new 一个 Empty 后缀为 .txt 文件,内容随笔拷贝一段 2.用 NSString 接收本地文件,再用一个标题拼接字符串 3.创建一个 NSMutableParagraphSty ...
- NSMutableAttributedString可变属性字符串的用法
适用于:当你想对一个字符串中的某几个字符更改颜色,字体... NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5 ...
- NSMutableAttributedString(富文本)的简单使用
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- NSMutableAttributedString
开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...
- UILabletext去掉乱码 控制颜色 行高 自定义大小 。显示不同的字体颜色、字体大小、行间距、首行缩进、下划线等属性(NSMutableAttributedString)
text去掉乱码 设置不同颜色 行高 自定义大小 #import <Foundation/Foundation.h> @interface TextsForRow : NSObject @ ...
- iOS使用NSMutableAttributedString实现富文本小结
NSAttributedString NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距).NSAttributedString对象的默 ...
- 富文本处理NSMutableAttributedString
概述 富文本处理在项目中使用率越来越高.比如像颜色改变突出, 大字号突出处理, 下划线处理, 中划线(删除线)处理等等 详细 代码下载:http://www.demodashi.com/demo/10 ...
- 快速创建各种类型的NSAttributeString和NSMutableParagraphStyle
NSDictionary *attributes = @{ NSForegroundColorAttributeName : [ UIColorredColor ], NSFontAttribut ...
随机推荐
- 九度OJ 1063:整数和 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3456 解决:2254 题目描述: 编写程序,读入一个整数N. 若N为非负数,则计算N到2N之间的整数和: 若N为一个负数,则求2N到N之间 ...
- Kubernetes TensorFlow 默认 特定 集群管理器
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...
- 阿里云ecs docker使用(2)
1. 退出docker容器 命令 exit 2.sudo docker ps -l 3. sudo docker images 4. sudo docker commit ba300f05c1a3 c ...
- SPDIF接口细则详解
链接:https://max.book118.com/html/2017/0422/101658483.shtm
- 用c++后缀自动机实现最大公共字符串算法,并封装成Python库
后缀自动机的C++代码转自https://e-maxx.ru/algo/suffix_automata,其余封装为自写. 在C++文件同级目录建立setup.py文件,代码如下: # !/usr/bi ...
- selenium 页面超时后捕获异常也无法继续get(url)使用的问题解决方案
参考这篇博客 http://www.xiaomilu.top/archives/106
- 数据解析,懒加载,代理ip
在前面的requests流程中,还缺少了一步重要的流程,就是在持久化存储之前需要进行制定的数据解析.因为在大多数情况下,我们都会使用聚焦爬虫,也就是爬取页面中的指定部分数据值,而不是整个页面的数据. ...
- Java多线程系列 基础篇06 synchronized(同步锁)
转载 http://www.cnblogs.com/paddix/ 作者:liuxiaopeng http://www.infoq.com/cn/articles/java-se-16-synchro ...
- Contiki Timer & Stimer 模块
一.Timer API struct timer { clock_time_t start; clock_time_t interval; }; CCIF void timer_set(struct ...
- 9.1 NOIP普及组试题精解(3)
9-6 seat.c #include <stdio.h> #define MAXN 1001 void swap(int *a, int *b) //交换数据 { int t; t = ...