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 ...
随机推荐
- jquery获取form表单中的内容,并将表单内容更新到datagrid的一行
//执行不刷新页面更新所修改的行 var arr = $('#patient_form').serializeArray();//将表单中的数据格式化成数组 var m = new Array(); ...
- Django框架创建数据库表时setting文件配置_模型层
若想将模型转为mysql数据库中的表,需要在settings中配置: 一. 确保配置文件中的INSTALLED_APPS中写入我们创建的app名称-->bms INSTALLED_APPS = ...
- Linux入门基础(三)——系统命令
- MySQL——存储引擎
核心知识点 1.InnoDB:数据和索引存放在单独的文件,聚簇索引,行级锁,事务,MVCC 2.MyISAM: (1)缺点:不支持事务和表级锁,因为不支持表锁,锁颗粒比较大,因此适合只读和小文件. ( ...
- 基于ajax和Form实现的注册
注册 urls.py from django.contrib import admin from django.urls import path, re_path from app01 import ...
- 【LeetCode-easy】Merge Two Sorted Lists
思路:指针p用于串联怎个链表,比较两个指针的大小,连接较小的一个.如果一个链表到达链尾,连接另外一个链表余下来的所以节点. public ListNode mergeTwoLists(ListNode ...
- P3209 [HNOI2010]平面图判定
P3209 [HNOI2010]平面图判定 哈密尔顿环之外的任意一条边,要么连在环内部,要么连在环外部 判断两条边在同一部分会相交,则这两条边必须分开 那么把边看作点连边,跑二分图染色就行 #incl ...
- 20145239 《Java程序设计》第9周学习总结
20145239 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 1.JDBC是java联机数据库的标准规范,它定义了一组标准类与接口,应用程序需要联机数 ...
- 深入理解JVM - Java内存模型与线程 - 第十二章
Java内存模型 主内存与工作内存 Java内存模型主要目标:定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节.此处的变量(Variable)与Java编程中 ...
- 分享知识-快乐自己:SpringMvc后台Date对象数据 到 前台页面的显示转换
常常为日期格式的转换而烦恼吗?那么就试试看看楼主的方式吧!让你摆脱烦恼,从而快乐撸码. 如果你只用作于一个日期的显示采用方式如下: 导入:相应的类库 <%@ taglib uri="h ...