ContextLabel 1.6.0

ContextLabel 1.6.0

测试测试
语言语言 SwiftSwift
许可 MIT
发布最新发布2019年5月
SPM支持 SPM

Michael LoistlMichael Loistl 维护。



  • 作者:
  • Michael Loistl

Build Status

ContextLabel

一个简单的 UILabel 替代品,用 Swift 编写,提供自动检测链接(如 URL、电话号码、Twitter 风格的用户名和标签)。

如何在项目中使用它

ContextLabel 没有任何特殊依赖,只需将 ContextLabel.swift 文件包含到您的项目中,然后用 ContextLabel 类替换 UILabel

委托 & 关闭

ContextLabel 支持委托和关闭,一旦设置了委托,就忽略关闭。

文本字体

从版本 1.3 开始,您可以通过在关闭或委托中返回一个字体,可选地为每个链接结果设置不同的字体。

contextLabel.textFont = { (linkResult) in
  return UIFont.systemFont(ofSize: 16)
}
func contextLabel(_ sender: ContextLabel, textFontForLinkResult linkResult: LinkResult) -> UIFont {
  return UIFont.systemFont(ofSize: 16)
}

文本颜色

ContextLabel支持为URL、Twitter风格用户名和哈希标签使用不同的颜色。默认情况下,链接文本颜色设置为 Closure 时用户处理RGB(71,90,109),标签RGB(151,154,158)和URL/电子邮件/文本链接RGB(45,113,178)。

要设置自己的文本颜色,在闭包或委托中返回所需的UIColor

contextLabel.foregroundColor = { (linkResult) in
	switch linkResult.detectionType {
  case .userHandle:
    return UIColor(red: 71.0/255.0, green: 90.0/255.0, blue: 109.0/255.0, alpha: 1.0)
  case .hashtag:
    return UIColor(red: 151.0/255.0, green: 154.0/255.0, blue: 158.0/255.0, alpha: 1.0)
  case .url, .email:
    return UIColor(red: 45.0/255.0, green: 113.0/255.0, blue: 178.0/255.0, alpha: 1.0)
  case .textLink:
    return UIColor(red: 45.0/255.0, green: 113.0/255.0, blue: 178.0/255.0, alpha: 1.0)
  case .phoneNumber:
    return UIColor(red: 45.0/255.0, green: 113.0/255.0, blue: 178.0/255.0, alpha: 1.0)
  default:
    return .black
  }
}
func contextLabel(_ sender: ContextLabel, foregroundColorForLinkResult linkResult: LinkResult) -> UIColor {
  return sender.foregroundColor(linkResult)
}

高亮文本颜色

如果没有从 foregroundHighlightedColor 返回 UIKit,当检测到链接时,将应用设置文本颜色的 alpha 为0.5。

要设置自己的文本高亮颜色,在闭包或委托中返回所需的 UIKit

contextLabel.foregroundHighlightedColor = { (linkResult) in
  return .lightGray
}
func contextLabel(_ sender: ContextLabel, foregroundHighlightedColorForLinkResult linkResult: LinkResult) -> UIColor {
  return sender.foregroundHighlightedColor(linkResult)
}

下划线样式

默认情况下,检测到的链接没有下划线。

要设置自己的下划线样式,在闭包或委托中返回所需的 NSUnderlineStyle

contextLabel.underlineStyle = { (linkResult) in
  return .styleNone
}
func contextLabel(_ sender: ContextLabel, underlineStyleForLinkResult linkResult: LinkResult) -> NSUnderlineStyle
  return sender.underlineStyle(linkResult)
}

修改属性字符串

从版本1.3开始,您也可以通过以下闭包或委托在属性字符串最终应用于标签之前对其进行修改

contextLabel.modifiedAttributedString = { (attributedString) in
  var _attributedString = attributedString

  // Modify attributes

  return _attributedString
}
func contextLabel(_ sender: ContextLabel, modifiedAttributedString attributedString: NSAttributedString) -> NSAttributedString {
  var _attributedString = modifiedAttributedString

  // Modify attributes

  return _attributedString
}

选择处理

当点击标签时,对于每个触摸状态都会调用闭包 didTouch: (TouchResult) -> Void

didTouch: { [weak self] (touchResult) in
    switch touchResult.state {
    case .began:
        // Do something
    case .ended:
        // Do something
    default:
        break
    }
}

从版本1.3.0开始,您可以另外实现委托

func contextLabel(_ sender: ContextLabel, didTouchWithTouchResult touchResult: TouchResult) {
    switch touchResult.state {
    case .began:
        // Do something
    case .ended:
        // Do something
    default:
        break
    }
}

触摸行为从版本 1.2.0 开始改变。

touchResult 包含您需要针对所选字符串采取行动所需的一切。

public struct TouchResult {
    public let linkResult: LinkResult?
    public let touches: Set<UITouch>
    public let event: UIEvent?
    public let state: UIGestureRecognizerState
}
public struct LinkResult {
    public let detectionType: ContextLabel.LinkDetectionType
    public let range: NSRange
    public let text: String
    public let textLink: TextLink?
}
public enum LinkDetectionType {
    case none
    case userHandle
    case hashtag
    case url
    case email
    case textLink
    case phoneNumber
}

已复制

要允许通过长按复制文本到剪贴板,请设置 contextLabel.canCopy = true 并实现以下闭包或委托

contextLabel.didCopy = { (text) in
  
}
func contextLabel(_ sender: ContextLabel, didCopy text: String?) {
  
}

文本链接

ContextLabel会自动识别以 # 和 @ 开头的单词以及手动定义的文本链接。

文本链接至少由一个字符串 “文本链接” 和一个当用户触摸定义好的文本时会被调用的动作(闭包)组成。从版本 0.3.1 开始,在标签文本中会识别给定文本的所有出现。为了限制在标签文本中的识别,可以在初始化 TextLink 时设置一个可选的范围。

TextLink(text: String, range: NSRange? = nil, options: NSString.CompareOptions = [], action: @escaping ()->())

缓存的使用

当设置文本时,ContextLabel会生成一个’ContextLabelData’的实例,它是NSObject子类,可以持久化存储,以便对未更改的数据进行复用。

“ContextLabelData”包含实际的attributedStringlinkRangeResults和一个可以用于与模型数据比较以查看其是否仍然有效的userInfo字典。

if let cachedContextLabelData: ContextLabelData =  {
	// Set cached contextLabelData
	contextLabel.contextLabelData = cachedTextContextLabelData
} else {
	// Set label text
	contextLabel.text = text

	// Cache contextLabelData that has been generated from contextLabel
	cacheContextLabelData(contextLabel.contextLabelData)
}          

维护期望

虽然总是欢迎贡献,但不要期望特性会远超当前的状态。如果您想添加新特性或需要修复和Swift更新,请随意Fork这个库或打开pull请求。

许可 & 信用

ContextLabel 在 MIT 许可证 下发布。ContextLabel 受 KILabel (https://github.com/Krelborn/KILabel) 启发。

联系