在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。NSMuttableAttstring(带属性的字符串),可以灵活实现以上功能。 NSMutableParagraphStyle段落风格,设置行间距、段间距、缩进、对齐方式等。

  1. 实例化方法和使用方法 实例化方法: 使用字符串初始化
  • (id)initWithString:(NSString *)str;//例:NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"文字样式处理"];

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

  • (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];

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

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

使用方法: 为某一范围内文字设置多个属性

  • (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;
  1. 常见的属性及说明

NSFontAttributeName //字体  NSParagraphStyleAttributeName //段落格式 NSForegroundColorAttributeName //字体颜色 NSBackgroundColorAttributeName //背景颜色  NSStrikethroughStyleAttributeName //删除线格式  NSUnderlineStyleAttributeName //下划线格式  NSStrokeColorAttributeName //删除线颜色  NSStrokeWidthAttributeName//删除线宽度 NSShadowAttributeName //阴影 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

两个样式枚举
typedef NS_ENUM(NSInteger, NSLineBreakMode) {/* What to do with long lines */
NSLineBreakByWordWrapping = 0, /* Wrap at word boundaries, default */NSLineBreakByCharWrapping,/* Wrap at character boundaries */
NSLineBreakByClipping,/* Simply clip */剪掉后面显示不了的部分
NSLineBreakByTruncatingHead,/* Truncate at head of line: "...wxyz" */头部分的内容以……方式省略
NSLineBreakByTruncatingTail,/* Truncate at tail of line: "abcd..." */结尾部分的内容以……方式省略
NSLineBreakByTruncatingMiddle/* Truncate middle of line: "ab...yz" */中间部分的内容以……方式省略
} NS_ENUM_AVAILABLE_IOS(6_0);
typedef NS_ENUM(NSInteger, NSWritingDirection) {
NSWritingDirectionNatural = -1, // Determines direction using the Unicode Bidi Algorithm rules P2 and P3
NSWritingDirectionLeftToRight = 0, // Left to right writing direction 左到右的书写方向
NSWritingDirectionRightToLeft = 1 // Right to left writing direction 右到左的书写方向
} NS_ENUM_AVAILABLE_IOS(6_0);
复制代码
/* NSFontAttributeName 字体大小
NSParagraphStyleAttributeName 段落的风格(设置首行,行间距,对齐方式什么的)
NSKernAttributeName 字间距 */NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle,
NSKernAttributeName:@(10),
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
复制代码

再来个实例

//随便定义一个字符串,做测试使用
NSString *massageStr = @"\t北京乌托邦有限公司(以下简称“乌托邦”)在此特别提醒您(用户)在注册成为用户之前,请认真阅读本《用户协议》(以下简称“协议”),确保您充分理解本协议中各条款。请您审慎阅读并选择接受或不接受本协议。除非您接受本协议所有条款,否则您无权注册、登录或使用本协议所涉服务。您的注册、登录、使用等行为将视为对本协议的接受,并同意接受本协议各项条款的约束。\r\n \t本协议约定乌托邦与用户之间关于“乌托邦”软件服务(以下简称“服务”)的权利义务。“用户”是指注册、登录、使用本服务的个人。本协议可由乌托邦随时更新,更新后的协议条款一旦公布即代替原来的协议条款,恕不再另行通知,用户可在本网站查阅最新版协议条款。在乌托邦修改协议条款后,如果用户不接受修改后的条款,请立即停止使用乌托邦提供的服务,用户继续使用乌托邦提供的服务将被视为接受修改后的协议。";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:massageStr];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:20];
//调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.agreementText length])];
[attributedString addAttribute:NSForegroundColorAttributeName value:
[UIColor redColor] range:NSMakeRange(0, 12)]; // 0为起始位置 length是从起始位置开始 设置指定颜色的长度
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(18, 3)]; //设置尺寸
[attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(18, 3)]; // 0为起始位置 length是从起始位置开始 设置指定字体尺寸的长度
复制代码

根据字符串获取宽高 根据宽度求高度 content 计算的内容 width 计算的宽度 font字体大小

+ (CGFloat)getHeightWithContent:(NSString *)content width:(CGFloat)width font:(CGFloat)font{
CGRect rect = [content boundingRectWithSize:CGSizeMake(width, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:font]}  context:nil];
return rect.size.height;
}
复制代码

根据高度度求宽度 content 计算的内容 Height 计算的高度 font字体大小

+ (CGFloat)getWidthWithContent:(NSString *)content height:(CGFloat)height font:(CGFloat)font{
CGRect rect = [content boundingRectWithSize:CGSizeMake(999, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
return rect.size.width;
}
复制代码

实际上,设置删除线

NSStrikethroughColorAttributeName 的时候,值也是这个枚举。// NSUnderlineStyleNone 不设置下划线/删除线// NSUnderlineStyleSingle 设置下划线/删除线为细的单线// NSUnderlineStyleThick 设置下划线/删除线为粗的单线// NSUnderlineStyleDouble 设置下划线/删除线为细的双线// NSUnderlinePatternSolid 设置下划线/删除线样式为连续的实线// NSUnderlinePatternDot 设置下划线/删除线样式为点,也就是虚线,比如这样:------// NSUnderlinePatterDash 设置下划线/删除线样式为破折号,比如这样:—— —— ——// NSUnderlinePatternDashDot 设置下划线/删除线样式为连续的破折号和点,比如这样:——-——-——-// NSUnderlinePatternDashDotDot 设置下划线/删除线样式为连续的破折号、点、点,比如:——--——--——--// NSUnderlineByWord 在有空格的地方不设置下划线/删除线
在设置以上属性时,可以以|分隔,同时设置多个属性
复制代码

iOS 文字样式处理总结(字体、前背景色、斜体、加粗、对齐、行间距、段间距、动态获取字符串label宽高等)...相关推荐

  1. 【Android studio】将字体设为斜体/加粗

    方法: android:textStyle="italic/bold" 如图 设为斜体

  2. C#实现利用单选框实现更改文本的richTextBox字体、大小、加粗

    功能 C#实现利用单选框实现更改文本的richTextBox字体.大小.加粗 程序 using System; using System.Collections.Generic; using Syst ...

  3. [iOS]修改UILabel的行间距/段间距/缩进

    [iOS]修改UILabel的行间距/段间距/缩进 - (void)viewDidLoad {[super viewDidLoad];UILabel *dLabel = _desLab;NSStrin ...

  4. CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)与数学表达式设置

    文字样式1(内嵌HTML) Size:规定文本的尺寸大小,取值从 1 到 7 ,浏览器默认值是 3. <font face="黑体">我是黑体字</font> ...

  5. 文字描边加粗_用AE制作动态手写文字效果

    现在很多设计师在设计logo或者字体的时候,已经不单纯的停留在固定不动的状态,而是添加了很多动效来提升作品的视觉效果.以现在流行的动效设计来看,可以分为三类:交互动效.MG动效.后期动效. 交互动效: ...

  6. HTML超链接文字加粗,Markdown语法之--标题/注释/超链接/下划线/图片/代码/贯穿线/斜体加粗/列表,使你的文本更丰富...

    为什么要使用Markdown语法编译我们的文本? 很不喜欢有的人写文章,从前到后全都是小四号,密密麻麻的,看到这我想你也会和我一样丧失掉继续看下去的兴趣了吧: 如果你的文章标题是标题,段落时段落,链接 ...

  7. 【5步解决】在 Word 中批量调整 Mathtype 所有公式格式(变量斜体加粗、字体缩小为5号等)的方法

        在写论文的时候,往往最后需要格式要求根据要求来调整 Word 中的 Mathtype输入公式的格式,包括变量字体格式.字体大小.变量是否加粗.是否斜体等内容.而一个一个手动改很繁琐,因此这里便 ...

  8. Latex学习笔记 05 字体、字号以及加粗等字体风格设置

    字体(Front fammilies) 默认情况下,字体是Roman系列的字体.如果需要临时使用其他种类的字体,在overleaf里直接使用像如下的指令即可: In this example, a c ...

  9. android 字体选中加粗,Android——TabLayout设置选中字体变大,加粗,透明度

    TabLayout自带没有设置选中时字体大小的属性,网上搜了好多基本都不生效,搞了一上午终于好使了,记个笔记记录下,布局要自己定义下,要不不好使. 布局中使用TabLayout的xml文件 andro ...

最新文章

  1. 前端中会用到的设计模式之单一职责原则
  2. python socket coding
  3. 解析ArcFace源码
  4. java关键字及其作用解释_java 常见关键字及其作用
  5. OpenFOAM流固耦合问题-FsiFoam(foam-extend-4.0)运行tutorials的bug修复
  6. Shell入门(二)之变量
  7. js html异步加载的属性,异步加载JS的五种方式
  8. 为什么老司机开车都不快?
  9. 如何在mysql命令窗口获取到程序正在执行的sql语句
  10. [Linux学习]更改默认启动图形界面或命令行
  11. 最近点对问题C语言源代码
  12. c语言delay函数的作用,delay函数 delay() c语言延迟函数
  13. A2 雷达多点触控
  14. XML第十讲:XML中Schema深入详解、元素、属性、关系
  15. centos7安装/卸载pip
  16. wps软件打不开共享超链接_wps excel的超链接打不开怎么办?
  17. 自定义typecho handsome主题标签云颜色
  18. detecting android sdk, Select Android SDK directory
  19. μCOS-Ⅱ操作系统中的PEND(阻塞zuse)和SUSPEND(挂起)和SLEEP(睡眠)三种状态
  20. 【XJTUSE计算机图形学】第三章 几何造型技术(3)——B样条曲线与曲面

热门文章

  1. apache ignite系列(九):ignite调优
  2. html5上传使用FileReader,Promise图片预览
  3. 学习日报 7-10(验证码)
  4. Android学习笔记View的工作原理
  5. codeforces 749
  6. 【emWin】例程一:emWin系列教程简介
  7. 017 在SecureCRT中安装rz小工具
  8. JavaScript执行bat文件清理浏览器缓存
  9. cos,sina,tan,cot
  10. 【转载】socket select模型