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))

最终效果示例

  • 删除线和样式效果
  • 描边和阴影
  • 段落间距与居中显示
  • 自定义排版方向和段落缩进

以上内容可根据实际需求进行扩展和调整,建议在使用前根据具体项目需求进行适当试验和优化。

上一篇:iOS ImageView控件
下一篇:iOS UILabel控件

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月19日 05时42分09秒