Texty 0.2.7

Texty 0.2.7

测试已测试
语言语言 SwiftSwift
许可 BSL-3-Clause
发布最后发布2022 年 1 月
SPM支持 SPM

CoryTexty.podspecJeff Meador 维护。



Texty 0.2.7

Build Status Platform CocoaPods Compatible Carthage Compatible

Texty

Texty 的目标是使文本样式化变得简单和干净。这通过使用样式容器、样式标签初始化器和类似 XML 的标签进行字符串样式化来实现。

由 Vectorform, LLC 创建和维护。

要求

  • iOS 8.0+
  • Xcode 10.0+
  • Swift 5.0+

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

Texty 需要 CocoaPods 1.0.0+ 版本来构建。

要将Texty集成到您的Xcode项目中并使用CocoaPods,请在您的Podfile中指定它。

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Texty', '~> 0.2.6'
end

接着,运行以下命令

$ pod install

Carthage

Carthage是一个去中心化的依赖管理器,它构建您的依赖并提供二进制框架。

您可以使用以下命令通过Homebrew安装Carthage

$ brew update
$ brew install carthage

要使用Carthage将Texty集成到您的Xcode项目中,请在您的Cartfile中指定它。

github "Vectorform/Texty" ~> 0.2.6

运行carthage update来构建框架,并将构建的Texty.framework拖到您的Xcode项目中。

手动集成

如果您不想使用所列出的任何依赖管理器,您可以将Texty手动集成到项目中。


使用方法

创建样式

TextStyle类被设计为创建一次并在整个应用程序中重用。如果您需要不改变原始样式来操作TextStyle,您需要使用copy初始化器UITextStyle(with: TextStyle)

/// Create static references to reusable styles
struct Style {
    static let Header1: TextStyle = TextStyle(attributes: [.foregroundColor : UIColor.black, .font : UIFont.boldSystemFont(ofSize: 24.0)])

    static let Header2: TextStyle = TextStyle(attributes: [.foregroundColor : UIColor.black, .font : UIFont.boldSystemFont(ofSize: 20.0)])

    static let Normal: TextStyle = TextStyle(attributes: [.foregroundColor : UIColor.black, .font : UIFont.systemFont(ofSize: 17.0)])

    static let Underline: TextStyle = TextStyle(attributes: [TextAttribute.underlineStyle : NSUnderlineStyle.styleSingle])
}
/// Reuse your defined styles across your entire application
class ViewController: UIViewController {
    private let headerLabel: TextyLabel = TextyLabel(style: Style.Header1)
    private let textLabel: TextyLabel = TextyLabel(style: Style.Normal)
}

可用的属性

TextAttribute 期望类型 本地等效
attachment NSTextAttachment NSAttributedStringKey.attachment
backgroundColor UIColor NSAttributedStringKey.backgroundColor
baselineOffset NSNumber NSAttributedStringKey.baselineOffset
expansion NSNumber NSAttributedStringKey.expansion
font UIFont NSAttributedStringKey.font
foregroundColor UIColor NSAttributedStringKey.foregroundColor
kern NSNumber NSAttributedStringKey.kern
ligature NSNumber NSAttributedStringKey.ligature
link NSURL或NSString NSAttributedStringKey.link
斜率 NSNumber NSAttributedStringKey.obliqueness
段落样式 NSParagraphStyle NSAttributedStringKey.paragraphStyle
阴影 NSShadow NSAttributedStringKey.shadow
删除线颜色 UIColor NSAttributedStringKey.strikethroughColor
删除线样式 NSNumber NSAttributedStringKey.strikethroughStyle
描边颜色 UIColor NSAttributedStringKey.strokeColor
描边宽度 NSNumber NSAttributedStringKey.strokeWidth
文本效果 NSString NSAttributedStringKey.textEffect
下划线颜色 UIColor NSAttributedStringKey.underlineColor
下划线样式 NSNumber NSAttributedStringKey.underlineStyle
垂直字形 NSNumber NSAttributedStringKey.verticalGlyphForm
书写方向 Array NSAttributedStringKey.writingDirection

有关每个属性的更多信息可以在 Apple 的 文档 中找到。

TextyLabel

TextyLabel 是一个针对 TextStyle 对象而创建的 UILabel 的子类。TextyLabel 的核心力量来源于其初始化器。TextyLabel 将会复制 TextStyle 对象。

let titleLabel: TextyLabel = TextyLabel(style: Style.Header1)

您可以通过 style 属性来修改或替换样式。

当子类化 TextyLabel 时请小心,因为一些属性被覆盖以从关联的样式对象而不是原生位置引用。

在不调用父类的 strong 的情况下子类化 TextyLabel 并覆盖这些属性之一将会导致未定义的行为。

属性 覆盖的目标
font style.font
lineBreakMode style.paragraphStyle.lineBreakMode
text attributedText
textAlignment style.paragraphStyle.alignment
textColor style.foregroundColor

TextyButton

TextyButton 是一个针对 TextStyle 对象而创建的 UIButton 的子类。TextyButton 可以像 TextyLabel 一样初始化。内部将使用这种样式为所有按钮状态。TextyButton 将会复制 TextStyle 对象。

let button: TextyButton = TextyButton(style: Style.Header1)

您可以通过以下函数后来修改或替换样式

style(for state: UIControlState)
setStyle(_ style: TextStyle, for state: UIControlState)

当子类化 TextyButton 时请小心,因为一些属性被覆盖以从关联的样式对象而不是原生位置引用。

在不调用父类的 strong 的情况下子类化 TextyButton 并覆盖这些属性之一将会导致未定义的行为。

函数 覆盖的目标
setTitle(_ title: String?, for state: UIControlState) setAttributedTitle(_ title: NSAttributedString?, for state: UIControlState)
title(for state: UIControlState) attributedTitle(for: state)
setTitleColor(_ color: UIColor?, for state: UIControlState) style(for: state).foregroundColor
titleColor(for state: UIControlState) style(for: state).foregroundColor
setTitleShadowColor(_ color: UIColor?, for state: UIControlState) style(for: state).shadow
titleShadowColor(for state: UIControlState) style(for: state).shadow

TextyTextView

TextyTextView 是为与 TextStyle 对象协同工作而创建的 UITextView 子类。TextyTextView 的核心力量来自其初始化器。TextyTextView 将会创建 TextStyle 对象的副本。

let titleLabel: TextyLabel = TextyLabel(style: Style.Header1)

TextyTextView 目前不支持编辑文本,因此你应该将 isEditable 设置为 false

您可以通过 style 属性来修改或替换样式。

在继承 TextyTextView 时需要小心,因为一些属性被重写以便从关联的样式对象而不是其原始位置引用。

在不调用超类的情况下继承 TextyTextView 并覆盖其中任何一个属性将导致未定义的行为。

属性 覆盖的目标
font style.font
lineBreakMode style.paragraphStyle.lineBreakMode
text attributedText
textAlignment style.paragraphStyle.alignment
textColor style.foregroundColor

通过标签样式文本

Texty 允许使用 XML 风格的标签在字符串中样式化部分内容。

let titleLabel: TextyLabel = TextyLabel(style: Style.Header1)
self.titleLabel.style.setStyle(Style.Underline, forTag: "underline")
self.titleLabel.text = "This is a <underline>TextyLabel</underline>"

你还可以跳过创建 TextStyle,直接使用属性字典。

let titleLabel: TextyLabel = TextyLabel(style: Style.Header1)
self.titleLabel.style.setAttributes([TextAttribute.underlineStyle : NSUnderlineStyle.styleSingle.rawValue], forTag: "underline")
self.titleLabel.text = "This is a <underline>TextyLabel</underline>"

与 XML 不同,标签不必成对出现。例如,以下字符串是有效的(假设粗体和斜体标签已被定义):

这是一个 <italic>示例 <bold>字符串用于</italic>演示</bold>目的。

请确保你的结束标签在前面有正斜杠,而不是在后面。

好的 差的
</bold> <bold/>

标签后面的正斜杠将会导致标签被检测为 短标签,这在将来可能会有一用,但目前没有任何作用。

目前没有办法在字符串中转义标签——所有标签都会在样式化过程中被移除。


致谢

Igor Efremov[email protected]

Jeff Meador[email protected]

许可证

Texty 适用于 BSD-3-Clause 许可证。更多信息请参阅 LICENSE 文件。