나는 다음과 같은 방법을 포함 UILabel의의 확장 생성 :스위프트 3에서 UILabel에서 해시 태그를 클릭 할 수있게하려면 어떻게해야합니까? 내 응용 프로그램에서
func formatTextInLabel() {
let text = self.text
let font = UIFont(name: fontName, size: 16.0)
let titleDict: NSDictionary = [NSFontAttributeName: font!]
// This will give me an attributedString with the desired font
let attributedString = NSMutableAttributedString(string: text!, attributes: titleDict as! [String : AnyObject])
let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: [])
let matches = regex!.matches(in: text!, options: [], range: NSMakeRange(0, text!.characters.count))
for match in matches {
let matchRange = match.rangeAt(0)
let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor]
attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
}
self.attributedText = attributedString
}
을하고 내 선택한 색상의 모든 #hashtags
을 강조한다. 하지만 그들은 클릭 할 수 없습니다, 나는 그것을 달성하기 불가능하다는 것을 말하는 그래서 어떤 게시물을 발견 : How to detect and make hyperlinks/mentions/hashtags clickable in UILabel?하지만 그것은 (잘하면) 구식 것 같습니다. 내가 어떻게하면 좋을지 알려줄 수 있니? 선택한 해시 태그의 이름을 콘솔에 표시 하시겠습니까?
추가 할 것은 한 가지 더 - pod
같은 문자가 ActiveLabel
또는 그와 비슷한 것으로 알고 있지만 시도해 보았고 제 경우의 제약 조건에 문제가있어서 제 코드에서이 효과를 얻고 싶습니다. UITapGestureRecognizer
라벨에 추가
해시 태그 –