RegexAttributer 1.1

RegexAttributer 1.1

rajavijayakumar维护。



  • 作者
  • Raja Vijaya Kumar

RegexAttributer

RegexAttributer是一个简单的、轻量级的基于给定正则表达式的属性字符串生成器。

作为移动开发者,我们在项目中可能需要在一个字符串上进行正则表达式验证,并给该特定匹配单独赋予一些属性。例如:应该渲染为##粗体##。 -> 应该渲染为粗体

有时我们可能需要根据渲染的正则表达式高亮字符串执行一些基于该字符串的操作。例如:{-点击这里-}(https://www.google.com)查看网站。- -> 点击这里查看网站。

这就是RegexAttributer派上用场的时候。

用法

import RegexAttributor

// Confirm to RegexAttributable protocol for enums, structs or classes. It should also confirm to Hashable Protocol

enum RegexAttributerType: RegexAttributable {
	
	case bold, highlight, url
	
	var regex: String {
		switch self {
			
			// **Bold**
			case .bold:
				return "\\*\\*(?<X>.*?)\\*\\*"
				
			// #Highlighter
			case .highlight:
				return "\\#(?<X>.*?).\\h"
				
			// [Click Here](https://www.google.com/)
			case .url:
				return "(\\[(?<X>.*?)\\])(\\()(http|https)?:\\/\\/([-\\w\\.]+)+(:\\d+)?(\\/([\\w|\\-\\/_\\.]*(\\?\\S+)?)?)?(\\))"
		}
	}
	
	var attributes: [NSAttributedString.Key : Any] {
		switch self {
			case .bold:
				return [.font: UIFont.boldSystemFont(ofSize: 18)]
			case .highlight:
				return [.font: UIFont.boldSystemFont(ofSize: 18),
						.foregroundColor: UIColor.darkGray]
			case .url:
				return [.foregroundColor: UIColor.blue,
						.underlineStyle: NSUnderlineStyle.thick.rawValue]
		}
	}
	
  // Here provide the function with your own logic on how will your regex matched string should be replaced and what should be the string retrived if 
  // any action is to be given for the rendered text.
  // (i.e):- Take URL for example
  // [Click Here](https://www.google.com/) is the match found for our Regex
  // we should replace 'Click Here' and store the url to perform any action when 'Click Here' is tapped
  // So perform your logic and return the replaced string in #1 tuple parameter and the url in #2 tuple parameter
	func generateTextRange(_ string: String) -> (String, String) {
		
		switch self {
			
			// **Bold** -> (Bold, Bold)
			case .bold:
				var boldString = string
				boldString.removeFirst(2)
				boldString.removeLast(2)
				return (boldString, boldString)
				
			// #Highlight -> (#Highlight, #Highlight)
			case .highlight:
				return (string, string)
			
			// [Click Here](https://www.google.com/) -> (Click Here, https://www.google.com/)
			// Click Here will be replaced in place of this regex and the url will be available in two places
			// RegexAttributerManager.getTextRanges(for key:) [NSRange(location: 0, length: 10): "https://www.google.com/"]
			// RegexAttributerManager.getAbsoluteValues(for key:) ["https://www.google.com/": "Click Here"]
			case .url:
				let splitedUrl = TextHightlighterHelper().splitUrl(from: string)
				return (splitedUrl.absoluteUrl, splitedUrl.prefix)
		}
	}
}

确认类型后,您可以创建RegexAttributerManager实例

let string = "This is an **Example** for regex and making the regex attributed string. [Here](https://www.google.com/) is a link embedded where **clicking on it** revels the Corresponding URL. [Clicking Here](http://www.youtube.com) will reveal another url #Easier_Regex_Management using **RegexAttributer**"

let initialAttr: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 18), .foregroundColor: UIColor.black]
let regexManager = RegexAttributerManager<RegexAttributerType>(string: string, textHighlightable: [.bold, .url, .highlight], initialAttributes: initialAttr)
let textView = UITextView()
textView.attributedText = regexManager.getAttributedString()

// regexManager.getTextRanges(for: .url) gives
// [{69, 4}: "https://www.google.com/", {144, 13}: "http://www.youtube.com"]

// regexManager.getAbsoluteValues(for: .url)
// ["http://www.youtube.com": "Clicking Here", "https://www.google.com/": "Here"]

安装指南

RegexAttributor可在CocoaPods中找到。将其添加到Podfile中

use_frameworks!

	pod 'RegexAttributer'
  
end

然后运行pod install。

许可

RegexAttributer遵循MIT许可证发布。有关更多信息,请参阅License.md