使用NSAttributedString可以进行个性化的文本属性显示设置。

NSAttributedString在使用时,共有多个属性可以行设置使用。

// 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
NSFontAttributeNameNSMutableAttributedString *textFont = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体大小"];
[textFont addAttribute:NSFontAttributeNamevalue:[UIFont boldSystemFontOfSize:18.0]range:[@"NSAttributedString设置字体大小" rangeOfString:@"NSAttributedString"]];

// 设置文本段落排版格式,取值为 NSParagraphStyle 对象(详情见下面的API说明)
NSParagraphStyleAttributeNameNSMutableParagraphStyle *rowParagraph = [[NSMutableParagraphStyle alloc] init];
rowParagraph.firstLineHeadIndent = 20;
rowParagraph.lineSpacing = 10;
NSMutableAttributedString *textRowSpace = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体文字间距"];
[textRowSpace addAttribute:NSParagraphStyleAttributeName value:rowParagraph range:NSMakeRange(0, textRowSpace.string.length)];

// 设置字体颜色,取值为 UIColor对象,默认值为黑色
NSForegroundColorAttributeNameNSMutableAttributedString *textColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体颜色"];
[textColor addAttribute:NSForegroundColorAttributeNamevalue:[UIColor greenColor]range:[@"NSAttributedString设置字体颜色" rangeOfString:@"NSAttributedString"]];

// 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
NSBackgroundColorAttributeNameNSMutableAttributedString *textBackColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体背景颜色"];
[textBackColor addAttribute:NSBackgroundColorAttributeNamevalue:[UIColor yellowColor]range:[@"NSAttributedString设置字体背景颜色" rangeOfString:@"NSAttributedString"]];

// 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
NSLigatureAttributeName
// 设置字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
NSKernAttributeNameNSMutableAttributedString *textSizeSpace = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体文字间距"];
[textSizeSpace addAttribute:NSKernAttributeNamevalue:@(2)range:[@"NSAttributedString设置字体文字间距" rangeOfString:@"NSAttributedString"]];

// 设置删除线,取值为 NSNumber 对象(整数)
NSStrikethroughStyleAttributeName// 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
NSStrikethroughColorAttributeNameNSMutableAttributedString *textDeleteline = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置删除线"];
[textDeleteline addAttribute:NSStrikethroughStyleAttributeNamevalue:@(NSUnderlineStyleSingle)range:[@"NSAttributedString设置删除线" rangeOfString:@"NSAttributedString"]];
[textDeleteline addAttribute:NSStrikethroughColorAttributeNamevalue:[UIColor blueColor]range:[@"NSAttributedString设置删除线" rangeOfString:@"NSAttributedString"]];

// 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
NSUnderlineStyleAttributeName// 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
NSUnderlineColorAttributeNameNSMutableAttributedString *textUnderline = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置下划线"];
[textUnderline addAttribute:NSUnderlineStyleAttributeNamevalue:@(NSUnderlineStyleSingle)range:[@"NSAttributedString设置下划线" rangeOfString:@"NSAttributedString"]];
[textUnderline addAttribute:NSUnderlineColorAttributeNamevalue:[UIColor brownColor]range:[@"NSAttributedString设置下划线" rangeOfString:@"NSAttributedString"]];

// 设置笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
NSStrokeWidthAttributeNameNSMutableAttributedString *textBorderType = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体笔画样式"];
[textBorderType addAttribute:NSStrokeWidthAttributeNamevalue:@(2)range:[@"NSAttributedString设置字体笔画样式" rangeOfString:@"NSAttributedString"]];

// 填充部分颜色,不是字体颜色,取值为 UIColor 对象
NSStrokeColorAttributeNameNSMutableAttributedString *textBorderColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体笔画颜色"];
[textBorderColor addAttribute:NSStrokeColorAttributeNamevalue:[UIColor greenColor]range:[@"NSAttributedString设置字体笔画颜色" rangeOfString:@"NSAttributedString"]];

// 设置阴影属性,取值为 NSShadow 对象
NSShadowAttributeNameNSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 1;
shadow.shadowColor = [UIColor purpleColor];
shadow.shadowOffset = CGSizeMake(3, 3);
//
NSMutableAttributedString *textShadow = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体阴影"];
[textShadow addAttribute:NSShadowAttributeNamevalue:shadowrange:[@"NSAttributedString设置字体阴影" rangeOfString:@"NSAttributedString"]];

// 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
NSTextEffectAttributeName
// 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSBaselineOffsetAttributeNameNSMutableAttributedString *textOffset = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString调整基线位置"];
[textOffset addAttribute:NSBaselineOffsetAttributeNamevalue:@(6)range:[@"NSAttributedString调整基线位置" rangeOfString:@"NSAttributedString"]];

// 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
NSObliquenessAttributeNameNSMutableAttributedString *textObliqueness = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置文本斜体"];
[textObliqueness addAttribute:NSObliquenessAttributeNamevalue:@(0.5)range:[@"NSAttributedString设置文本斜体" rangeOfString:@"NSAttributedString"]];

// 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSExpansionAttributeNameNSMutableAttributedString *textExpansion = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置文本压缩"];
[textExpansion addAttribute:NSExpansionAttributeNamevalue:@(0.5)range:[@"NSAttributedString设置文本压缩" rangeOfString:@"NSAttributedString"]];

// 设置文字书写方向,从左向右书写或者从右向左书写
NSWritingDirectionAttributeNameNSString *string = @"NSWritingDirectionAttributeName书字方向";
NSMutableAttributedString *attibutedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attibutedStr addAttribute:NSWritingDirectionAttributeNamevalue:@[@(3)]range:NSMakeRange(0, string.length)];

// 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSVerticalGlyphFormAttributeNameNSString *string = @"NSVerticalGlyphFormAttributeName排版方向";
NSMutableAttributedString *attibutedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attibutedStr addAttribute:NSVerticalGlyphFormAttributeNamevalue:@(3)range:NSMakeRange(0, string.length)];

// 设置链接属性,点击后调用浏览器打开指定URL地址
NSLinkAttributeNameNSMutableAttributedString *textLink = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置超链接"];
[textLink addAttribute:NSLinkAttributeNamevalue:[NSURL URLWithString:@"http://www.baidu.com"]range:[@"NSAttributedString设置超链接" rangeOfString:@"NSAttributedString"]];

// 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSAttachmentAttributeNameNSTextAttachment *attachmentImage = [[NSTextAttachment alloc] init];
attachmentImage.image = [UIImage imageNamed:@"image"];
attachmentImage.bounds = CGRectMake(0, 0, 20.0, 20.0);
NSAttributedString *attributedImage = [NSAttributedString attributedStringWithAttachment:attachmentImage];
//
NSMutableAttributedString *textImage = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString图文混排"];
[textImage insertAttributedString:attributedImage atIndex:[@"NSAttributedString图文混排" rangeOfString:@"NSAttributedString"].length];

NSAttributedString文本属性设置的使用相关推荐

  1. CSS -- CSS字体及文本属性设置总结

    文章目录 1 CSS字体属性 1.1 字体系列 1.2 字体大小 1.3 字体粗细 1.4 文字样式 1.5 字体复合属性 1.6 字体属性总结 2 CSS文本属性 2.1 文本颜色 2.2 对齐文本 ...

  2. android设置文本大小属性,TextView文本属性设置

    在Android中,TextView是我们最常用的用来显示文本的控件. 一般情况下,TextView中的文本都是一个样式.那么如何对于TextView中各个部分的文本来设置字体,大小,颜色,样式,以及 ...

  3. 【MATLAB】基本绘图 ( 图形属性 | 绘图对象 | 图形属性界面 | 坐标轴属性 | 线属性 | 文本属性 | 图形属性设置策略 )

    文章目录 一.图形属性 1.图形属性列举 2.绘图对象 二.图形属性设置 1.图形属性界面 2.坐标轴属性 3.线属性 4.文本属性 三.图形属性设置策略 一.图形属性 1.图形属性列举 图形属性 : ...

  4. html语言填充没有只有描边,HTML5 Canvas笔记——交互绘制文本(描边、填充、阴影、渐变填充、图案填充、文本的属性设置)...

    (1)文本的描边.填充.阴影 (2)文本的渐变填充 (3)文本的图案填充 (4)文本的属性设置及效果呈现 交互绘制文本.html 交互绘制文本 body { background: #eeeeee; ...

  5. html设置文本域的,HTML-文本域属性设置

    1.设置文本域的字体 输入内容 2.设置文本域的背景色 输入内容 3.文本域的虚线边框设置 输入内容 4.文本域的实线边框设置 输入内容 5.文本域的点线边框设置 输入内容 6. 设置文本域的背景图 ...

  6. Qt 设置Excel单元格文本属性

    前段时间通过Qt设置Excel的内容,每次设置 1-3 这种内容的时候,会被Excel自动转为日期格式. 后来查了一下,可以设置单元格为文本属性,显示内容与输入内容保持一致. 需要先设置单元格的格式再 ...

  7. html中文本属性的设置,css 文本属性direction的功能详解说明

    摘要: 下文讲述css属性direction的功能说明,如下所示: css direction的功能说明 css direction的功能: 设置html元素中文本流的显示方向 css directi ...

  8. 如何设置文本不换行省略号显示等CSS常用文本属性

    如何让多余的文本省略号显示首先要说几个属性的作用: whitespace:nowrap 中文行末不断行显示 overflow: 控制超出文本的显示方式:hidden 超出范围文本隐藏:scroll 始 ...

  9. css中字体与段落属性设置/文本高级样式

    CSS中字体与段落属性 毫无疑问,不管什么网站,文字一定是必不可少.文字可以是网页传播信息的主要手段.那么怎么显示文字,才能更加的美观,那么大家需要了解以下文字属性. 字体属性 属性 用途 语法(一些 ...

最新文章

  1. pythonpil库过滤图像contour_快乐python 零基础也能P图 —— PIL库
  2. 笔记 - AliCloud Auto Scaling(弹性伸缩)简介
  3. ftp: connect: 没有到主机的路由_Qt之FTP上传/下载文件操作
  4. Spring MVC上下文父子容器
  5. JDK 14 / JEP 305模式匹配“ Smart Casts”实例
  6. OpenGL ES 纹理设置
  7. 将字符串编码成 GBK
  8. 红米Note 7 Pro在印度首销迅速售罄
  9. php对象、面向对象
  10. 解决在IE6下使用display: inline-block;的不兼容性问题
  11. python jupyter notebook 上传文件_使用jupyter notebook将文件保存为Markdown,HTML等文件格式...
  12. 皮尔逊相关系数php,若两变量X和y之间的Pearson相关系数大于0.3且小于0.5,则说明()。...
  13. 智慧校园家校综合信息化管理系统平台
  14. webpack打包UMD学习记录
  15. 国外游戏开发公司10强
  16. linux集群高可用--crm
  17. PVM and MPI 比较
  18. 马化腾动怒!微信数据“被共享”,山寨微信团伙被判一年!
  19. 叁拾伍- Django Websocket 绝望之旅(dwebsocket 以及 channels)
  20. vagrant的同步文件配置,配置虚拟机ip映射

热门文章

  1. Linux入门基础知识
  2. shell的建立与执行实验报告_【实验报告】练习二:shell编程
  3. C#线程锁(自旋锁SpinLock、互斥锁Mutex、混合锁Monitor | lock)
  4. iOS vs Android苹果将重蹈PC业务覆辙?
  5. centos安装使用puppeteer和headless chrome
  6. linux—pwd命令详解(一天一个命令)
  7. 合并或注销重复领英帐号
  8. 请问,张颂文、淄博烧烤、奇瑞汽车之间有什么共同点
  9. 线稿图视频制作--从此短视频平台不缺上传视频了
  10. 如何利用互联网工具深入调研你仰慕已久的网站