XDXLabelFlow
Table of contents - 目录
简介
GitHub 仓库 XDXLabelFlow最初受GitHub用户 ZhuPeng1314 编写的 MSSAutoresizeLabelFlow 的启发,并在此基础上增加了以下功能和改进:
- 除了 Objective-C 外,使用 Swift 4.1 编写;
- @IBDesignable 类和 @IBInspectable 属性,使得开发者可以在 Xcode 模板或 xib 文件中设置属性并预览 UI 行为;
- 支持 CocoaPods 安装;
- 与 MSSAutoresizeLabelFlow 相比更容易使用。
安装
在 iOS 项目中安装此 API 最简单的方法是将所有类型为 .swift 的文件复制到 Xcode 项目文件夹中。
使用 Cocoapods 安装
target '<your_project>' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for <your_project>
pod 'XDXLabelFlow'
end
提示
安装步骤
- 在 xib / storyboard 文件中,将一个 UIView 拖到一个视图中,并将类名重命名為 XDXLabelFlow。
- 将 XDXLabelFlow 对象链接到该类。示例
@IBOutlet weak var labelFlow: XDXLabelFlow!
- 使用 set(data: [String], handler: ((Int, String) -> Void)? = nil) 方法,XDXLabelFlow 实例准备显示并响应用户点击事件。示例
labelFlow.set(data: ["Apple", "Google", "Microsoft", "Visa", "MasterCard", "American Express"]) { (index, title) in
print("The selected title is \(title) in index \(index)")
}
注意:以下所有方法,请确保在视图出现在屏幕后才使用,以避免崩溃。
- 使用 insertLabel(with: title: String, at: index: Int, animated: Bool) 方法插入标签。示例
labelFlow.insertLabel(with: "Alibaba", at: 3, animated: true)
- 使用 deleteLabel(at: index: Int, animated: Bool) 方法删除标签。示例
labelFlow.deleteLabel(at: 3, animated: true)
- 使用 reloadAll(with: titles: [String]) 方法重新加载所有标题。示例
labelFlow.reloadAll(with: ["Visa", "MasterCard", "American Express", "JCB", "UnionPay"])
全局配置
XDXLabelFlowManager 是一个类,包含一个单例实例,用于在项目中配置所有的 XDXLabelFlow 实例。下面是一个示例
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
configXDXLabelFlowGlobally()
return true
}
func configXDXLabelFlowGlobally()
{
XDXLabelFlowManager.shared.contentInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
XDXLabelFlowManager.shared.itemLabelTextFont = UIFont.systemFont(ofSize: 15)
XDXLabelFlowManager.shared.labelTextMargin = 20
XDXLabelFlowManager.shared.gapBetweenLines = 10
XDXLabelFlowManager.shared.gapBetweenItems = 10
XDXLabelFlowManager.shared.itemHeight = 25
XDXLabelFlowManager.shared.itemCornerRadius = 3
XDXLabelFlowManager.shared.itemBackgroundColor = .white
XDXLabelFlowManager.shared.itemLabelTextColor = .darkText
XDXLabelFlowManager.shared.backgroundColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1)
}
只需将此段代码复制到项目中并稍作修改,不需要在创建 XDXLabelFlow 时在代码文件或 xib / storyboard 文件中进行任何更改。XDXLabelFlowManager 中的所有变量都是可选的,因此您不必在 AppDelegate 中这样做。
简介
该 GitHub 仓库 XDXLabelFlow 是基于 ZhuPeng1314 的仓库 MSSAutoresizeLabelFlow 改编并强化之作:
- 用 Swift 4.1 编写,而不是 Objective-C;
- @IBDesignable 和 @IBInspectable 可以让开发者设置 XDXLabelFlow 的属性并预览 UI 效果;
- 可以使用 Cocoapods 导入至项目中;
- 比 MSSAutoresizeLabelFlow 更简单易用。
安装说明
将 XDXLabelFlow 安装到 iOS 项目的最简单方法是将目录下的所有 .swift 文件直接复制到 Xcode 项目文件夹中。
安装Cocoapods:
target '<你的项目名>' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for <你的项目名>
pod 'XDXLabelFlow'
end
上手步骤
- 在 xib / storyboard 编辑器中,引入 UIView 并将其重命名为 XDXLabelFlow。
- 将 XDXLabelFlow 连接到类的代码中。以下代码示例:
@IBOutlet weak var labelFlow: XDXLabelFlow!
- 使用 set(data: [String], handler: ((Int, String) -> Void)? = nil) 方法后,XDXLabelFlow 实例可以正确显示并响应用户点击。以下代码示例:
labelFlow.set(data: ["Apple", "Google", "Microsoft", "Visa", "MasterCard", "American Express"]) { (index, title) in
print("The selected title is \(title) in index \(index)")
}
注意事项:请确保在视图显示在屏幕上后再调用以下方法以避免系统崩溃。
- 使用 insertLabel(with title: String, at index: Int, animated: Bool) 方法来插入标签。以下代码示例:
labelFlow.insertLabel(with: "Alibaba", at: 3, animated: true)
- 使用 deleteLabel(at index: Int, animated: Bool) 方法来删除标签。以下代码示例:
labelFlow.deleteLabel(at: 3, animated: true)
- 使用 reloadAll(with titles: [String]) 方法来重新加载所有标签。以下代码示例:
labelFlow.reloadAll(with: ["Visa", "MasterCard", "American Express", "JCB", "UnionPay"])
全局设置类 XDXLabelFlowManager 包含一个单例,用于配置项目中所有的 XDXLabelFlow 实例。以下是一个示例:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
configXDXLabelFlowGlobally()
return true
}
func configXDXLabelFlowGlobally()
{
XDXLabelFlowManager.shared.contentInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
XDXLabelFlowManager.shared.itemLabelTextFont = UIFont.systemFont(ofSize: 15)
XDXLabelFlowManager.shared.labelTextMargin = 20
XDXLabelFlowManager.shared.gapBetweenLines = 10
XDXLabelFlowManager.shared.gapBetweenItems = 10
XDXLabelFlowManager.shared.itemHeight = 25
XDXLabelFlowManager.shared.itemCornerRadius = 3
XDXLabelFlowManager.shared.itemBackgroundColor = .white
XDXLabelFlowManager.shared.itemLabelTextColor = .darkText
XDXLabelFlowManager.shared.backgroundColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1)
}
将以上代码复制到 iOS 项目的 AppDelegate 文件中,并根据需要进行修改。这样就不需要在代码、xib 或 storyboard 文件中重复配置 XDXLabelFlow 了。该类内的变量都是 optional,因此以上代码不需要出现在 iOS 项目中。
支持 XDXLabelFlow
- ★Star该repo
联系
- LinkedIn: @Chong Xie
许可XDXLabelFlow 采用 MIT 许可。请参阅 LICENSE 文件获取更多信息。
@IBOutlet weak var labelFlow: XDXLabelFlow!
labelFlow.set(data: ["Apple", "Google", "Microsoft", "Visa", "MasterCard", "American Express"]) { (index, title) in
print("The selected title is \(title) in index \(index)")
}
labelFlow.insertLabel(with: "Alibaba", at: 3, animated: true)
labelFlow.deleteLabel(at: 3, animated: true)
labelFlow.reloadAll(with: ["Visa", "MasterCard", "American Express", "JCB", "UnionPay"])
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
configXDXLabelFlowGlobally()
return true
}
func configXDXLabelFlowGlobally()
{
XDXLabelFlowManager.shared.contentInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
XDXLabelFlowManager.shared.itemLabelTextFont = UIFont.systemFont(ofSize: 15)
XDXLabelFlowManager.shared.labelTextMargin = 20
XDXLabelFlowManager.shared.gapBetweenLines = 10
XDXLabelFlowManager.shared.gapBetweenItems = 10
XDXLabelFlowManager.shared.itemHeight = 25
XDXLabelFlowManager.shared.itemCornerRadius = 3
XDXLabelFlowManager.shared.itemBackgroundColor = .white
XDXLabelFlowManager.shared.itemLabelTextColor = .darkText
XDXLabelFlowManager.shared.backgroundColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1)
}