1.NSDataDetector是继承于NSRegularExpression(Cocoa中的正则表达式)的一个子类。系统帮做好的一些正则匹配。
public static var orthography: NSTextCheckingResult.CheckingType { get } // language identification
public static var spelling: NSTextCheckingResult.CheckingType { get } // spell checking
public static var grammar: NSTextCheckingResult.CheckingType { get } // grammar checking
public static var date: NSTextCheckingResult.CheckingType { get } // date/time detection
public static var address: NSTextCheckingResult.CheckingType { get } // address detection
public static var link: NSTextCheckingResult.CheckingType { get } // link detection
public static var quote: NSTextCheckingResult.CheckingType { get } // smart quotes
public static var dash: NSTextCheckingResult.CheckingType { get } // smart dashes
public static var replacement: NSTextCheckingResult.CheckingType { get } // fixed replacements, such as copyright symbol for (c)
public static var correction: NSTextCheckingResult.CheckingType { get } // autocorrection
@available(iOS 4.0, *)
public static var regularExpression: NSTextCheckingResult.CheckingType { get } // regular expression matches
@available(iOS 4.0, *)
public static var phoneNumber: NSTextCheckingResult.CheckingType { get } // phone number detection
@available(iOS 4.0, *)
public static var transitInformation: NSTextCheckingResult.CheckingType { get } // transit (e.g. flight) info detection
2.初始化方法:
public init(types checkingTypes: NSTextCheckingTypes) throws
3.几个调用的方法:
open func matches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [NSTextCheckingResult] //返回匹配到的数组
open func numberOfMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> Int //返回匹配到的个数
open func firstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSTextCheckingResult? //返回第一个匹配到的结果
open func rangeOfFirstMatch(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> NSRange //返回第一个匹配到结果的范围
4.使用:
lazy var showlabel:UILabel = {
let string = "电话号码是15812345678还是15012345678\n网址是www.baidu.com"
let showLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
showLabel.center = self.view.center
showLabel.font = UIFont.systemFont(ofSize: 14)
showLabel.text = string;
showLabel.numberOfLines = 0
return showLabel
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUp()
}
func setUp() {
self.view.addSubview(self.showlabel)
let string = self.showlabel.text! as String
let types: NSTextCheckingResult.CheckingType = [.phoneNumber,.link]
let detector:NSDataDetector = try!NSDataDetector(types: types.rawValue)
let attributeText = NSMutableAttributedString(string: string);
detector.enumerateMatches(in: string, options: NSRegularExpression.MatchingOptions(rawValue:0), range: NSMakeRange(0, (string as NSString).length), using: {
(_result:NSTextCheckingResult?, _flags:NSRegularExpression.MatchingFlags, _:UnsafeMutablePointer<ObjCBool>) -> Void in
let range:NSRange = (_result?.range)!
print(_result ?? "");
if _result?.resultType == .link {
attributeText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: range)
attributeText.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: range)
}else if _result?.resultType == .phoneNumber {
attributeText.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: range)
}
self.showlabel.attributedText = attributeText
}
)
/*
* 返回数组匹配到的数组
*/
let matches = detector.matches(in: string, options: NSRegularExpression.MatchingOptions(rawValue:0), range: NSMakeRange(0, (string as NSString).length));
for result:NSTextCheckingResult in matches {
let range:NSRange = result.range
if result.resultType == .link {
attributeText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: range)
attributeText.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: range)
}else if result.resultType == .phoneNumber {
attributeText.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: range)
}
self.showlabel.attributedText = attributeText
}
5.实现效果: