HMTextView
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
要求
安装
手动
- 克隆此仓库
- 导航到项目文件夹
- 将
源
拷贝到您的项目中
使用CocoaPods
HMTextView可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod 'HMTextView'
用法
- 在您想要使用HMTextView的类中导入
import HMTextView
。 - 使用类
HMTextView
创建一个UITextView。 - 如有必要,更新配置参数。
代码
在创建HMTextView的视图控制器中,例如名为hmTextView
的
协议
HMTextView Specific
/// Returns all links in the text view
func links(hashtags: [String], mentions: [String])
/// Click event of a link
func clicked(on link: String, type: HMType)
/// When enter a'#' or '@'
func readyToEnter(link_with type: HMType)
/// When stopped entering a link
func stoppedEntering(link_with type: HMType)
/// When character limit reached
func charLimitReached()
/// If character limit will not be reached
func charLimitAvailable()
/// Chars written so far and remained char count
func chars(_ written: Int, _ remained: Int)
Common Methods
/// Same with textViewShouldBeginEditing
func shouldBeginEditing(_ textView: UITextView)
/// Same with textViewDidEndEditing
func didEndEditing(_ textView: UITextView)
/// Same with textViewDidBeginEditing
func didBeginEditing(_ textView: UITextView)
/// Same with textViewDidChangeSelection
func didChangeSelection(_ textView: UITextView)
/// Same with textViewShouldEndEditing
func shouldEndEditing(_ textView: UITextView)
/// Same with textViewDidChange
func didChange(_ textView: UITextView)
/// Same with shouldChangeTextIn
func shouldChangeTextIn(_ textView: UITextView,
_ range: NSRange,
_ replacementText: String,
_ returning: Bool)
配置
override func viewDidLoad() {
super.viewDidLoad()
hmTextViewDelegate = self
hmTextView.textAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)
]
hmTextView.linkAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.red
]
hmTextView.hashtagFont = UIFont.boldSystemFont(ofSize: 20)
hmTextView.mentionFont = UIFont.boldSystemFont(ofSize: 12)
}
配置参数
/// Main regex for detecting hashtag and mention. You Can update this with your own.<br/>
public var regex = "(?<=\\s|^)([@#][\\p{L}\\d]+[._]\\p{L}+|[#@][\\p{L}\\d]*)(?=[']\\p{L}+|[.,;:?!](?:\\s|$)|\\s|$)"
/// Line spacing of the paragraph style.
public var lineSpacing: CGFloat = 0
/// Letter spacing of the paragraph style.
public var kern: CGFloat = 0.0
/// Allow links start with numbers.
public var allowLinksStartWithNumber: Bool = false
/// Set minimum character count for links.
public var minimumLinkCharCount: Int = 3
/// Turn this to false if you don't want to highlight hashtags.
public var detectHashtags: Bool = true
/// Turn this to false if you don't want to highlight mentions.
public var detectMentions: Bool = true
/// Updates the hashtag font.
public var hashtagFont: UIFont = UIFont.boldSystemFont(ofSize: 17)
/// Updates the mention font.
public var mentionFont: UIFont = UIFont.boldSystemFont(ofSize: 17)
/// Update general text attributes.
public var textAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.black,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17)
]
/// Update link attributes.
public var linkAttributes: [NSAttributedString.Key: Any]! {
didSet {
self.linkTextAttributes = self.linkAttributes
}
}
/// Character count.
public var charCount: Int = -1
https://www.hackingwithswift.com/articles/108/how-to-use-regular-expressions-in-swift
https://stackoverflow.com/a/48938881
https://stackoverflow.com/a/24144365
作者
gokhanmandaci, [email protected]
许可证
HMTextView受MIT许可证的授权。更多信息请参阅LICENSE文件。