Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
自定义匹配URL正则表达式
  • Loading branch information
moQingTing committed Nov 25, 2016
commit 03b5bc4198864eff1922bd13f347683f43cc18b4
10 changes: 7 additions & 3 deletions HYLabel/Source/HYLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ extension HYLabel {
}

private func getLinkRanges() -> [NSRange]? {
// 创建正则表达式
guard let detector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue) else {
// 创建正则表达式 匹配URL
let pattern = "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?"

guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return nil
}

return getRangesFromResult(detector)


return getRangesFromResult(regex)
}

private func getRangesFromResult(regex : NSRegularExpression) -> [NSRange] {
Expand Down