StringStylizer 5.2.1

StringStylizer 5.2.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2020 年 3 月
SPM支持 SPM

Kazuhiro Hayashi 维护。



  • 作者:
  • Kazuhiro Hayashi

StringStylizer

Carthage compatible Cocoapods compatible Swift 5.0

类型严格的 NSAttributedString 构建类。

这是什么?

StringStylizer 通过方法链和操作符使 NSAttributedString 更直观。构建 NSAttributedString 非常困难,因为它需要我们记住属性名称和类型。如果您使用 StringStylizer 来这样做,就无需记住它们。😃

NSAttributedString 的格式如下。

let attr: [NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.white,
    .font: UIFont(name: "HelveticaNeue", size: 14)
]
let str = NSAttributedString(string: "some text", attributes: attr)

StringStylizer 允许开发者以线性方式读取和编写他们的代码。如果您想将具有一些颜色、大小和字体的字符串转换为 NSAttributedString,可以按照以下方式编写。

let str = "some text".stylize().color(.white).size(14).font(.HelveticaNeue).attr

功能

  • 类型严格格式
  • 以线性方式分配范围和属性
  • 比 NSAttributedString 更易于阅读

需求

  • 支持iOS 8.0及以上版本
  • Swift 5.0~ 或 Swift 4.0~ 或 Swift 3.2~

安装

Carthage

  • 使用Homebrew安装Carthage
> ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
> brew update
> brew install carthage
  • 移动到您的项目目录下并创建Cartfile文件
> touch Cartfile
  • 在Cartfile中添加以下行
github "kazuhiro4949/StringStylizer"
  • 创建框架
> carthage update --platform iOS
  • 在Xcode中,转到“通用 > 构建设置 > 链接的框架和库”
  • 将框架添加到您的项目中
  • 添加一个新的运行脚本并将以下代码放入其中
/usr/local/bin/carthage copy-frameworks
  • 在“输入文件”处点击“+”并添加框架路径
$(SRCROOT)/Carthage/Build/iOS/StringStylizer.framework
  • 在您的源文件中编写导入语句
import StringStylizer

CocoaPods

  • 安装CocoaPods
> gem install cocoapods
> pod setup
  • 创建Podfile文件
> pod init
  • 编辑Podfile文件
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'  # add
use_framework!  # add

target 'MyAppName' do
  pod 'StringStylizer' # add
end

target 'MyAppTests' do

end

target 'MyAppUITests'
  • 进行安装
> pod install

打开.xcworkspace文件

示例

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))

// build NSAttributedString.
let greeting = "Hi, ".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr

// build NSAttributedString with ranges.
let msg = "something happened ".stylize()
    .range(0..<9)        .color(0x009911).size(12).font(.HelveticaNeue)
    .range(10..<UInt.max).color(0xaa22cc).size(14).font(.HelveticaNeue_Bold).attr

// build NSAttributedString objects and join them.
let name = "to ".stylize().color(0x23abfc).size(12).font(.HelveticaNeue).attr +
    "you".stylize().color(0x123456).size(14).font(.HelveticaNeue_Italic).underline(.double).attr
    
// build NSAttributedString objects with strikethrough and kerning applied.
let response = "\nHow ".stylize().attr +  "boring".stylize().strikeThrough(.single).attr +
    " exciting!".stylize().kern(-2).attr

此示例生成了带有样式的标签。

当然,您可以将方法链包裹起来。

extension StringStylizer {
  func strong() -> NSAttributedString {
    return self
            .color(0x123456)
            .size(14)
            .font(.HelveticaNeue_Italic)
            .underline(.double)
            .attr
  }
}

label.attributedText = "you".stylize().strong()

用法

1. 将字符串转换为StringStylizer对象

let firstStep = "yay!".stylize() // => StringStylizer<Styling>
1a. 或者使用可选的字符串
let optionalString:String? = nil
let firstStep = optionalString.stylize() // => StringStylizer<Styling>

2. 调用方法选择范围。然后,StringStylizer进入“缩小”状态

let secondStep = "yay!".stylize().range(0..<UInt.max) // => StringStylizer<NarrowDown>

3. 调用方法设置属性。然后,StringStylizer进入“样式”状态

let thirdStep = "yay!".stylize().range(0..<UInt.max).size(14) // => StringStylizer<Styling>

4. 转换为NSAttributedString对象。

let fourthStep = "yay!".stylize().range(0..<UInt.max).size(14).attr // => NSAttributedString

5. 连接另一个NSAttributedString对象。

let one     = "yay!".stylize().range(0..<UInt.max).size(14).attr
let another = " yay!".stylize().color(0xffffff).attr
let fifthStep = one + another // => NSAttributedString

这就完成了!

架构

StringStylizer基于“建造者模式”(Java有效版本)。此外,它使用“幻影类型”管理 states。

正因为如此,我们才能够

  • 以线性方式编写代码
  • 根据实际情况调用适当的方法。

许可证

版权所有(c)2016 Kazuhiro Hayashi

在此特此免费准许任何获得本软件及其相关文档副本(以下简称"软件")的个人,不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,以及允许向软件提供副本的个人进行如此的处理的权利,前提是遵守以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按原样提供,无论何种方式,均不提供任何形式的保证,明示或暗示,包括但不仅限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权持有者都不应对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,此类索赔、损害或其他责任源自、源于或与软件或使用软件有关,或与之一起使用或在其他方面。