Attributed框架API详解:掌握所有属性设置方法
2026/7/4 6:10:56 网站建设 项目流程

Attributed框架API详解:掌握所有属性设置方法

【免费下载链接】Attributedµframework for Attributed strings.项目地址: https://gitcode.com/gh_mirrors/at/Attributed

Attributed是一个轻量级的Swift框架,专为简化富文本字符串(Attributed strings)的创建和管理而设计。本文将全面解析Attributed框架的核心API,帮助开发者快速掌握文本属性设置的所有方法,轻松实现精美的文本排版效果。

核心类与协议

Attributed类

Attributed类是框架的核心,通过泛型设计实现对基础类型的扩展。其构造方法如下:

public final class Attributed<Base> { public init(_ base: Base) { ... } }

该类通过包装基础对象(如StringNSAttributedString),提供链式调用的属性设置接口。

AttributedCompatible协议

为了使任意类型支持Attributed扩展,框架定义了AttributedCompatible协议:

public protocol AttributedCompatible { ... }

遵循此协议的类型可自动获得attributed属性,用于创建富文本对象。

Attributes结构体详解

Attributes结构体是设置文本样式的核心,提供了丰富的属性配置方法。以下是常用API分类说明:

初始化方法

public struct Attributes { // 空初始化 public init() { ... } // 闭包配置初始化 public init(_ attributesBlock: (Attributes) -> Attributes) { ... } }

字体与间距设置

  • 字体设置font(_ font: UIFont) -> Attributes
  • 字符间距kerning(_ kerning: Double) -> Attributes

文本装饰

  • 删除线strikeThroughStyle(_ strikeThroughStyle: NSUnderlineStyle) -> Attributes
  • 下划线underlineStyle(_ underlineStyle: NSUnderlineStyle) -> Attributes

颜色设置

  • 前景色foreground(color: UIColor) -> Attributes
  • 背景色background(color: UIColor) -> Attributes
  • 描边色strokeColor(_ strokeColor: UIColor) -> Attributes

段落样式

  • 行间距lineSpacing(_ lineSpacing: CGFloat) -> Attributes
  • 对齐方式alignment(_ alignment: NSTextAlignment) -> Attributes
  • 首行缩进firstLineHeadIndent(_ firstLineHeadIndent: CGFloat) -> Attributes

高级属性

  • 阴影效果shadow(_ shadow: NSShadow) -> Attributes
  • 链接设置link(_ link: String) -> Attributes
  • 基线偏移baselineOffset(_ offset: NSNumber) -> Attributes

字符串扩展API

框架为StringNSString提供了便捷扩展,直接创建富文本:

String扩展

// 使用属性字典创建 public func attributed(with attributes: Attributes) -> NSAttributedString // 使用闭包配置属性 public func attributed(_ attributeBlock: (Attributes) -> (Attributes)) -> NSAttributedString

使用示例

let styledText = "Hello World".attributed { $0.font(UIFont.systemFont(ofSize: 16)) .foreground(color: .blue) .kerning(2.0) }

操作符重载

为简化富文本拼接,框架重载了+操作符:

// 拼接两个富文本 public func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString // 合并属性 public func + (lhs: Attributes, rhs: Attributes) -> Attributes

实用技巧

链式调用组合

利用Attributes的方法链特性,可以快速构建复杂样式:

let titleStyle = Attributes() .font(.boldSystemFont(ofSize: 20)) .foreground(color: .darkGray) .alignment(.center) .lineSpacing(8)

段落样式快捷设置

除了直接设置NSParagraphStyle,框架提供了多个快捷方法:

  • lineSpacing(_:):设置行间距
  • paragraphSpacing(_:):设置段间距
  • lineBreakMode(_:):设置换行模式

测试与验证

框架提供了完整的单元测试,覆盖主要API功能:

  • AttributesTests:验证属性设置正确性
  • StringExtensionTests:测试字符串扩展方法
  • NSAttributedStringExtensionTests:验证富文本操作

测试文件位于项目的AttributedTests目录下,可参考测试用例了解API的具体使用场景。

总结

Attributed框架通过简洁的API设计,极大简化了iOS富文本开发流程。其核心优势在于:

  1. 链式语法:直观的属性配置方式
  2. 类型安全:编译时检查属性设置
  3. 扩展灵活:支持任意类型的富文本扩展

掌握本文介绍的API方法,开发者可以轻松实现各种复杂的文本样式,提升App的视觉表现力。框架源码位于项目根目录下,主要实现文件包括Attributes.swiftString+Attributed.swiftOperators.swift

【免费下载链接】Attributedµframework for Attributed strings.项目地址: https://gitcode.com/gh_mirrors/at/Attributed

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询