EasyAttributes
示例
要运行示例项目,请先克隆仓库,然后从示例目录中运行pod install
。
要求
iOS 10+
安装
EasyAttributes可以通过CocoaPods获取。要安装它,只需在Podfile中添加以下行即可
pod 'EasyAttributes'
用法
import UIKit
import EasyAttributes
class ViewController: UIViewController {
let label: UILabel = {
let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.numberOfLines = 0
lbl.textColor = .black
lbl.textAlignment = .center
lbl.font = UIFont.systemFont(ofSize: 16)
let string = "Hello <c:ebebeb>there</c> <b26><u>I am a bold string</u></b26> and <i14>i am an italic string</i14> Hello there <u>again</u>"
lbl.attributedText = string.toAttributed()
return lbl
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 70).isActive = true
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
}
结果
自定义字体
如果您想使用自定义字体,您必须
- 注册自定义字体。请在此处找到说明:[插入链接地址](https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app)
然后,在 ViewController...
import UIKit
import EasyAttributes
class ViewController: UIViewController {
let label: UILabel = {
let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.numberOfLines = 0
lbl.textColor = .black
lbl.textAlignment = .center
lbl.font = UIFont.systemFont(ofSize: 16)
EasyAttributes.configFonts(with: ["pr":"PermanentMarker-Regular"])
let string = "Hello <c:ebebeb>there</c> <b26><u>I am a bold string</u></b26> and <i14>i am an italic string</i14> Hello there <u>again</u> <pr36>this is custom font string</pr36>"
lbl.attributedText = string.toAttributed()
return lbl
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.heightAnchor.constraint(equalToConstant: 400).isActive = true
}
}
结果
UITextView可点击链接
import UIKit
import EasyAttributes
class ViewController: UIViewController {
lazy var textView: UITextView = {
EasyAttributes.configFonts(with: ["pr":"PermanentMarker-Regular"], fontAdaptationFactor: 1.1)
let string = "<pr25>System string click </pr25><a:chat>here</a>"
let textView = UITextView(
easyAttributesString: string,
font: UIFont(name: "PermanentMarker-Regular", size: 25)!,
textColor: .black,
backgroundColor: .white,
textAlignment: .left,
linkColor: .red
)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.delegate = self
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(textView)
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 400).isActive = true
}
}
extension ViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
if URL.absoluteString == "easy://chat" {
debugPrint("clicked a link")
}
return false
}
}
结果
作者
米哈利斯·卡拉吉奥格斯,[插入邮箱]([email protected])
许可协议
EasyAttributes 在 MIT 许可协议下可用。更多信息请参阅 LICENSE 文件。