支持标签 (#)、提及 (@) 的 UILabel 插入式替换,用 Swift 编写
富标签
UILabel 插入式替换支持标签 (#)、提及 (@)、URL (http://)、电子邮件和自定义正则表达式模式,用 Swift 编写
特征
- 斯威夫特 5.0+
- 默认支持标签、提及、链接、电子邮件
- 通过正则表达式支持自定义类型
- 能够仅为所需类型启用突出显示
- 修剪网址的能力
- 超级易用且重量轻
- 作为
UILabel
直接替代品 - 经过充分测试和记录
例子
要运行示例项目,请克隆 repo,然后pod install
首先从 Example 目录运行。
要求
安装
RichLabel 可通过CocoaPods获得。要安装它,只需将以下行添加到您的 Podfile 中:
pod 'RichLabel'
用法
import RichLabel
let label = RichLabel()
label.numberOfLines = 0
label.enabledTypes = [.mention, .hashtag, .url, .email]
label.text = "This is a post with #hashtags and a @userhandle."
label.textColor = .black
label.handleHashtagTap { hashtag in
print("Success. You just tapped the \(hashtag) hashtag")
}
自定义类型
let customType = RichType.custom(pattern: "\\swith\\b") //Regex that looks for "with"
label.enabledTypes = [.mention, .hashtag, .url, .email, customType]
label.text = "This is a post with #hashtags and a @userhandle."
label.customColor[customType] = UIColor.purple
label.customSelectedColor[customType] = UIColor.green
label.handleCustomTap(for: customType) { element in
print("Custom type tapped: \(element)")
}
启用/禁用突出显示
默认情况下,RichLabel 实例具有以下配置
label.enabledTypes = [.mention, .hashtag, .url, .email]
但请随意启用/禁用以满足您的要求
批量定制
在使用RichLabel时,推荐使用customize(block:)
自定义的方法。原因是 RichLabel 会对您设置的每个属性做出反应。所以如果你设置了3个属性,textContainer会被刷新3次。
使用 时customize(block:)
,您可以对标签上的所有自定义进行分组,这样 RichLabel 只会刷新 textContainer 一次。
例子:
label.customize { label in
label.text = "This is a post with #multiple #hashtags and a @userhandle."
label.textColor = UIColor(red: 102.0/255, green: 117.0/255, blue: 127.0/255, alpha: 1)
label.hashtagColor = UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)
label.mentionColor = UIColor(red: 238.0/255, green: 85.0/255, blue: 96.0/255, alpha: 1)
label.URLColor = UIColor(red: 85.0/255, green: 238.0/255, blue: 151.0/255, alpha: 1)
label.handleMentionTap { self.alert("Mention", message: $0) }
label.handleHashtagTap { self.alert("Hashtag", message: $0) }
label.handleURLTap { self.alert("URL", message: $0.absoluteString) }
}
修剪长网址
您可以设置 url 的最大长度;
label.urlMaximumLength = 30
从现在开始,比这更大的 url 将被修剪。
https://afancyurl.com/whatever
->https://afancyurl.com/wh...
应用程序接口
mentionColor: UIColor = .blueColor()
mentionSelectedColor: UIColor?
hashtagColor: UIColor = .blueColor()
hashtagSelectedColor: UIColor?
URLColor: UIColor = .blueColor()
URLSelectedColor: UIColor?
customColor: [RichType : UIColor]
customSelectedColor: [RichType : UIColor]
lineSpacing: Float?
handleMentionTap: (String) -> ()
label.handleMentionTap { userHandle in print("\(userHandle) tapped") }
handleHashtagTap: (String) -> ()
label.handleHashtagTap { hashtag in print("\(hashtag) tapped") }
handleURLTap: (NSURL) -> ()
label.handleURLTap { url in UIApplication.shared.openURL(url) }
handleEmailTap: (String) -> ()
label.handleEmailTap { email in print("\(email) tapped") }
handleCustomTap(for type: RichType, handler: (String) -> ())
label.handleCustomTap(for: customType) { element in print("\(element) tapped") }
handleOutsideTap: () -> ()
label.handleOutsideTap { print("OutsideTap tapped") }
filterHashtag: (String) -> Bool
label.filterHashtag { hashtag in validHashtags.contains(hashtag) }
filterMention: (String) -> Bool
label.filterMention { mention in validMentions.contains(mention) }
作者
fengming, 1028708571@qq.com
执照
RichLabel 在 MIT 许可证下可用。有关详细信息,请参阅许可证文件。