ActiveLabel 1.1.0

ActiveLabel 1.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2019年5月
SPM支持 SPM

Lasha EfremidzePol Quintana维护。



  • Optonaut

ActiveLabel.swift Carthage compatible Build Status

用于替换 UILabel 的标签库,支持 hashtags(#)、mentions(@)、urls(http://)和自定义正则表达式模式,使用 Swift 编写

功能

  • Swift 5.0 (1.1.0) 和 4.2 (1.0.1)
  • 默认支持 Hashtags, Mentions, Links
  • 通过正则表达式支持 自定义类型
  • 能够仅启用所需类型的突出显示
  • 能够裁剪 URL
  • 使用简单,体积轻巧
  • 作为 UILabel 的直接替换使用
  • 经过良好测试和文档记录

用法

import ActiveLabel

let label = ActiveLabel()
label.numberOfLines = 0
label.enabledTypes = [.mention, .hashtag, .url]
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 = ActiveType.custom(pattern: "\\swith\\b") //Regex that looks for "with"
label.enabledTypes = [.mention, .hashtag, .url, 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)") 
}

启用/禁用高亮显示

默认情况下,ActiveLabel 实例具有以下配置

label.enabledTypes = [.mention, .hashtag, .url]

可根据需求启用/禁用

批处理定制

使用 ActiveLabel 时,建议使用 customize(block:) 方法进行定制。原因在于 ActiveLabel 会针对每个设置的属性做出响应。因此,如果设置了3个属性,文本容器将被刷新3次。

使用 customize(block:) 时,可以将标签上的所有自定义组合在一起,这样可以确保 ActiveLabel 只刷新一次文本容器。

示例

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

可以设置 URL 的最大长度;

label.urlMaximumLength = 30

从现在起,超过该长度的 URL 将被缩短。

https://afancyurl.com/whatever -> https://afancyurl.com/wh...

API

mentionColor: UIColor = .blueColor()
提及颜色选择器颜色: UIColor?
哈希标记颜色: UIColor = .blueColor()
哈希标记选择器颜色: UIColor?
URL颜色: UIColor = .blueColor()
URL选择器颜色: UIColor?
自定义颜色: [ActiveType : UIColor]
自定义选择器颜色: [ActiveType : 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) }
handleCustomTap(for type: ActiveType, handler: (String) -> ())
label.handleCustomTap(for: customType) { element in print("\(element) tapped") }
filterHashtag: (String) -> Bool
label.filterHashtag { hashtag in validHashtags.contains(hashtag) }
filterMention: (String) -> Bool
label.filterMention { mention in validMentions.contains(mention) }

安装(iOS 8+)

Carthage

将以下内容添加到您的 Cartfile 文件中,并按照这些说明操作。

github "optonaut/ActiveLabel.swift"

CocoaPods

CocoaPods 0.36 添加了对 Swift 和嵌入式框架的支持。要将 ActiveLabel 集成到项目中,请将以下内容添加到您的 Podfile 文件中。

platform :ios, '10.0'
use_frameworks!

pod 'ActiveLabel'

其他选项

在开始编写 ActiveLabel 之前,我们已经尝试了许多以下替代方案,但对其质量水平或使用便捷性并不十分满意,因此我们决定贡献自己的解决方案。

  • TTTAttributedLabel(ObjC)- 一个支持属性、数据检测器、链接等的 UILabel 的替代方案
  • STTweetLabel(ObjC)- 一个支持点击 #标签页、@处理和链接的 UILabel
  • AMAttributedHighlightLabel(ObjC)- 一个支持提及/标签页/链接高亮的 UILabel 子类
  • KILabel(ObjC)- 一个易于使用的、适用于 iOS 7 及以上版本的简称替代方案,可突出显示链接,如 URL、Twitter 风格的用户名和标签页,并使其可触摸