
iOS AttributedString简介
发布日期:2021-05-14 01:35:25
浏览次数:14
分类:精选文章
本文共 2724 字,大约阅读时间需要 9 分钟。
NSAttributedString 和 NSMutableAttributedString 文本样式 开发指南
1. NSAttributedString 初始化
1.1 初始化方法
- initWithString:
- 使用字符串初始化,默认属性值。
let attrString = NSAttributedString(string: "简单字符串")
- initWithString:attributes:
- 使用字符串和属性字典初始化,可对部分文字应用属性。
let attrString = NSAttributedString(string: "带属性的字符串", attributes: [ NSFontAttributeName: UIFont(name: " bold", size: 20), NSForegroundColorAttributeName: UIColor.red])
- initWithAttributedString:
- 通过已有NSAttributedString对象初始化,支持复制并应用更多属性。
let newAttrString = NSAttributedString(string: "被初始化的字符串", attributedString: existingAttrString)
2. NSMutableAttributedString 方法
2.1 修改文本样式
- addAttribute:range:
- 为指定范围添加属性。
let mutableAttr = NSMutableAttributedString(string: "加粗删除线")mutableAttr.addAttribute(NSStrikethroughStyleAttributeName, value: NSUnderlineStyleSingle, range: NSRange(location:4, length:1))
- addAttributes:range:
- vồ此范围添加多个属性。
let mutableAttr = NSMutableAttributedString(string: "多个属性")let attributes = [ NSFontAttributeName: UIFont/UIFontRadius.., NSForegroundColor: UIColor.green]mutableAttr.addAttributes(attributes, range: NSRange(location:0, length:8))
- removeAttribute:range:
- 移除指定范围内属性。
let mutableAttr = ...mutableAttr.removeAttribute(NSStrikethroughStyleAttributeName, range: NSRange(location:4, length:1))
3. 文本样式属性说明
3.1 常用属性
-
字体属性
NSFontAttributeName
: 字体样式,默认是系统字体12号。NSFontSizeAttribute
: 字体大小。
-
颜色属性
NSForegroundColorAttributeName
: 文本颜色,默认黑色。NSStrikethroughForegroundColor
: 删划线颜色。NSBackgroundColorAttributeName
: 文本背景颜色,默认 nil。
-
排版属性
NSLigatureAttributeName
: 连体字处理,默认1(打开连体字)。NSSKernAttributeName
: 字距调整,单位为像素。
-
其他样式
NSStrikethroughStyleAttributeName
: 删划线样式,默认是NSUnderlineStyleNone
。NSUnderlineStyleAttributeName
: 下划线样式。
-
自定义样式
NSStrokeWidthAttributeName
: 描边宽度,默认 0。NSStrokeColorAttributeName
: 描边颜色,默认继承自NSForegroundColorAttributeName
。
3.2 特殊属性
- 阴影效果
NSShadowAttributeName
: 阴影属性,可自定义阴影位置和颜色。
- 文字排版方向
NSWritingDirectionAttributeName
: 设置文字书写方向,默认左到右。NSVerticalGlyphFormAttributeName
: 设置竖排或横排效果。
4. 段落样式与间距
4.1 段落属性
- 段落间距
NSParagraphStyleAttributeName
: 段落样式,默认是defaultParagraphStyle
。
4.2 自定义间距
- 行间距
lineSpacing
: 行间距。minimumLineHeight
: 最低行间距。
4.3 文本对齐
- textAlignment: 文本对齐方式,如
NSTextAlignmentCenter
。
示例代码
// 字体样式let fontText = [[NSMutableAttributedString alloc] initWithString: "25号粗体" ] fontsText.setAttribute(NSFontAttributeName, value: UIFont-boldSystemFontOfSize:25, range: NSMakeRange(4, 5))// 颜色和背景色let colorText = [[NSMutableAttributedString alloc] initWithString: "红色字体,蓝色背景" ]colorText.addAttribute(NSStrikethroughForegroundColor, value: red, range: NSMakeRange(0,4))colorText.addAttribute(NSStrikethroughBackgroundColor, value: blue, range: NSMakeRange(4,4))
最终效果示例
- 删除线和样式效果
- 描边和阴影
- 段落间距与居中显示
- 自定义排版方向和段落缩进
以上内容可根据实际需求进行扩展和调整,建议在使用前根据具体项目需求进行适当试验和优化。
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月19日 05时42分09秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
C++long long
2019-03-13
腾讯十年开发者发自腾讯一线的真实Android面试资料
2019-03-13
移动开发程序员的悲哀是什么?
2019-03-13
Android 架构组件 – 让天下没有难做的 App
2019-03-13
Android多种方式实现相机圆形预览 看这一篇就够了
2019-03-13
启动MongoDB出现1053错误
2019-03-13
flask_migrate的学习
2019-03-13
CASS秒绘1000+控制点的小技巧
2019-03-13
GIS应用技巧之迭代器(Model)
2019-03-13
怎么理解spring的事务传播行为?答案在这里@现男友
2019-03-13
冒泡排序
2019-03-13
C++:拷贝初始化与赋值的区别
2019-03-13
centOS7.6安装nginx
2019-03-13
每天记录一句代码
2019-03-13
[python复习6]正则表达式
2019-03-13
[python复习6]tcp三次握手,四次挥手
2019-03-13
django框架学习记录
2019-03-13
Flask框架基础使用
2019-03-13
Flask操作SQLAlchemy
2019-03-13