Last active
August 29, 2015 14:23
-
-
Save manchan/83dca87e8ad66c9c372b to your computer and use it in GitHub Desktop.
Swiftでテキスト内リンク&テキストタップ検出 ref: http://qiita.com/you_matz/items/bbf0e6632cc56823948a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
textView.userInteractionEnabled = true | |
textView.editable = false | |
// tapでテキストのポジション検出可能とするため | |
textView.selectable = true | |
// 行間レイアウト用 | |
// textView.layoutManager.delegate = self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let text = "Swiftでテキスト内リンク&テキストタップ検出" | |
let linkText = "リンク" | |
let nsTex = text as NSString | |
let link = text.rangeOfString(linkText) | |
let attributedString = NSMutableAttributedString(string: text) | |
// リンク位置範囲生成 startからlength | |
range = NSMakeRange(distance(text.startIndex, link!.startIndex), distance(link!.startIndex, link!.endIndex)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// テキスト全体文字色 | |
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, nsTex.length)) | |
// リンクカラー | |
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: range) | |
// リンク下線 | |
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: range) | |
// 属性を代入 | |
textView.attributedText = attributedString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gesture追加 | |
let tap = UITapGestureRecognizer(target: self, action: "tapText:") | |
textView.addGestureRecognizer(tap) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func tapText(tap: UITapGestureRecognizer) { | |
// タップされた座標をもとに最寄りの文字列の位置を取得 | |
let location = tap.locationInView(textView) | |
let textPosition = textView.closestPositionToPoint(location) | |
// テキストの先頭とタップした文字の距離をNSIntegerで取得 | |
let selectedPosition = firstTextView.offsetFromPosition(firstTextView.beginningOfDocument, toPosition: textPosition!) | |
// タップした文字がリンク文字のrangeに含まれるか判定 | |
if NSLocationInRange(selectedPosition, range) { | |
// リンクタップ時の処理 | |
// let webVC = WebViewController() | |
// self.presentViewController(webVC, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment