样式化 2.0.0

样式化 2.0.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017 年 9 月
SwiftSwift 版本4.0
SPM支持 SPM

Alex Fish 维护。



样式化 2.0.0

  • 作者:
  • alexfish

styize

是 NSAttributedString 的功能包装,用于轻松字符串样式化

目录

为什么 NSAttributedString 需要包装器?

使用 NSAttributedString 样式化字符串需要大量的痛苦和难看的代码,例如改变子字符串的颜色并加下划线需要

let string = NSMutableAttributedString(string: "Hello")
string.addAttribute(NSforegroundAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 5))
string.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, string.length))

ouch!

这很快就会变成一大块代码,难以阅读和维护。使用 stylize 后,我们的代码如下所示

let string          = NSAttributedString(string: "Hello World")
let foregroundStyle = Stylize.foreground(UIColor.redColor(), range: NSMakeRange(0, 5))
let underlineStyle  = Stylize.underline(NSUnderlineStyle.StyleSingle)
let style           = Stylize.compose(foregroundStyle, underlineStyle)

let styledString    = style(string)

这样就好了。

集成

手册

iOS 8+

  1. 请使用 git submodule add https://github.com/alexfish/stylize.git 将 Stylize 添加到您的项目作为子模块
  2. 打开 Stylize 文件夹并将 Stylize.xcodeproj 拖到您的项目树中
  3. Stylize.framework 添加到您的目标 Link Binary with Libraries 构建阶段
  4. 使用 import Stylize 添加 Stylize 并准备开始使用

使用方法

let string        = NSAttributedString(string: "Hello World")
let style         = Stylize.foreground(UIColor.redColor())
let styledString  = style(string)

子字符串

默认情况下,样式将应用于整个字符串;如果您需要将样式应用于子字符串,每个样式都提供了可选的 range 参数

let string        = NSAttributedString(string: "Hello World")
let style         = Stylize.foreground(UIColor.redColor(), range: NSMakeRange(0, 5))
let styledString  = style(string)

组合样式

stylize 有一个 compose 函数,可以组合多个样式

let string          = NSAttributedString(string: "Hello World")
let foregroundStyle = Stylize.foreground(UIColor.redColor())
let backgroundStyle = Stylize.background(UIColor.orangeColor())
let underlineStyle  = Stylize.underline(NSUnderlineStyle.StyleSingle)

let style           = Stylize.compose(foregroundStyle, backgroundStyle, underlineStyle)
let styledString    = style(string)

可用属性

属性 函数
NSUnderlineStyleAttributeName underline(style: NSUnderlineStyle)
NSUnderlineColorAttributeName underline(color: UIColor)
NSForegroundColorAttributeName foreground(color: UIColor)
NSBackgroundColorAttributeName background(color: UIColor
NSStrikethroughStyleAttributeName strikethrough(style: NSUnderlineStyle)
NSStrikethroughColorAttributeName strikethrough(color: UIColor)
NSLinkAttributeName link(url: NSURL)
NSParagraphStyleAttributeName paragraph(style: NSParagraphStyle)
NSKernAttributeName kern(points: NSNumber)
NSBaselineOffsetAttributeName baseline(offset: NSNumber)
NSShadowAttributeName shadow(shadow: NSShadow)
NSStrokeWidthAttributeName stroke(width: NSNumber)
NSStrokeColorAttributeName stroke(color: UIColor)
NSTextEffectAttributeName letterpress()
NSFontAttributeName font(font: UIFont)
NSLigatureAttributeName ligatures(enabled: Bool)
NSObliquenessAttributeName obliqueness(skew: NSNumber)
NSAttachmentAttributeName attachment(attachement: NSTextAttachment)
NSExpansionAttributeName expand(log: NSNumber)
NSWritingDirectionAttributeName direction(direction: WritingDirection)