mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-14 15:26:40 +10:00
new way for ChatTextView detect mention
This commit is contained in:
@@ -33,12 +33,14 @@ class ChatLeftTextCell: ChatBaseCell {
|
||||
|
||||
avatarImageView.center = CGPoint(x: YepConfig.chatCellGapBetweenWallAndAvatar() + halfAvatarSize, y: halfAvatarSize + topOffset)
|
||||
|
||||
/*
|
||||
textContentTextView.chatTextStorage.mentionForegroundColor = UIColor.yepTintColor()
|
||||
textContentTextView.linkTapEnabled = true
|
||||
|
||||
prepareForMenuAction = { [weak self] otherGesturesEnabled in
|
||||
self?.textContentTextView.linkTapGestureRecognizer?.enabled = otherGesturesEnabled
|
||||
}
|
||||
*/
|
||||
|
||||
textContentTextView.tapMentionAction = { [weak self] username in
|
||||
self?.tapUsernameAction?(username: username)
|
||||
|
||||
@@ -30,12 +30,14 @@ class ChatRightTextCell: ChatRightBaseCell {
|
||||
|
||||
avatarImageView.center = CGPoint(x: fullWidth - halfAvatarSize - YepConfig.chatCellGapBetweenWallAndAvatar(), y: halfAvatarSize)
|
||||
|
||||
/*
|
||||
textContentTextView.chatTextStorage.mentionForegroundColor = UIColor.whiteColor()
|
||||
textContentTextView.linkTapEnabled = true
|
||||
|
||||
prepareForMenuAction = { [weak self] otherGesturesEnabled in
|
||||
self?.textContentTextView.linkTapGestureRecognizer?.enabled = otherGesturesEnabled
|
||||
}
|
||||
*/
|
||||
|
||||
textContentTextView.tapMentionAction = { [weak self] username in
|
||||
self?.tapUsernameAction?(username: username)
|
||||
|
||||
@@ -8,6 +8,103 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
class ChatTextView: UITextView {
|
||||
|
||||
var mentionForegroundColor: UIColor = UIColor.redColor()
|
||||
var tapMentionAction: ((username: String) -> Void)?
|
||||
|
||||
static let detectionTypeName = "ChatTextStorage.detectionTypeName"
|
||||
|
||||
enum DetectionType: String {
|
||||
case Mention
|
||||
}
|
||||
|
||||
override func didMoveToSuperview() {
|
||||
super.didMoveToSuperview()
|
||||
|
||||
self.delegate = self
|
||||
}
|
||||
|
||||
override var text: String! {
|
||||
didSet {
|
||||
let attributedString = NSMutableAttributedString(string: text)
|
||||
|
||||
let textRange = NSMakeRange(0, (text as NSString).length)
|
||||
|
||||
let mentionPattern = "@[^\\s::,,@]+$?"
|
||||
|
||||
let mentionExpression = try! NSRegularExpression(pattern: mentionPattern, options: NSRegularExpressionOptions())
|
||||
|
||||
mentionExpression.enumerateMatchesInString(text, options: NSMatchingOptions(), range: textRange, usingBlock: { result, flags, stop in
|
||||
|
||||
if let result = result {
|
||||
let textValue = (self.text as NSString).substringWithRange(result.range)
|
||||
|
||||
let textAttributes: [String: AnyObject] = [
|
||||
NSForegroundColorAttributeName: self.mentionForegroundColor,
|
||||
NSLinkAttributeName: textValue,
|
||||
ChatTextView.detectionTypeName: DetectionType.Mention.rawValue,
|
||||
]
|
||||
|
||||
attributedString.addAttributes(textAttributes, range: result.range )
|
||||
}
|
||||
})
|
||||
|
||||
self.attributedText = attributedString
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: 点击链接 hack
|
||||
|
||||
override func canBecomeFirstResponder() -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
override func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
|
||||
|
||||
// iOS 9 以上,强制不添加文字选择长按手势,免去触发选择文字
|
||||
// 共有四种长按手势,iOS 9 正式版里分别加了两次:0.1 Reveal,0.12 tap link,0.5 selection, 0.75 press link
|
||||
if isOperatingSystemAtLeastMajorVersion(9) {
|
||||
if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer {
|
||||
if longPressGestureRecognizer.minimumPressDuration == 0.5 {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.addGestureRecognizer(gestureRecognizer)
|
||||
}
|
||||
}
|
||||
|
||||
extension ChatTextView: UITextViewDelegate {
|
||||
|
||||
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
|
||||
|
||||
guard let detectionTypeName = self.attributedText.attribute(ChatTextView.detectionTypeName, atIndex: characterRange.location, effectiveRange: nil) as? String, detectionType = DetectionType(rawValue: detectionTypeName) else {
|
||||
return true
|
||||
}
|
||||
|
||||
let text = (self.text as NSString).substringWithRange(characterRange)
|
||||
|
||||
self.hangleTapText(text, withDetectionType: detectionType)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private func hangleTapText(text: String, withDetectionType detectionType: DetectionType) {
|
||||
|
||||
println("hangleTapText: \(text), \(detectionType)")
|
||||
|
||||
let username = text.substringFromIndex(text.startIndex.advancedBy(1))
|
||||
|
||||
if !username.isEmpty {
|
||||
tapMentionAction?(username: username)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// MARK: ChatTextStorage
|
||||
|
||||
class ChatTextStorage: NSTextStorage {
|
||||
@@ -262,3 +359,4 @@ extension ChatTextView: UIGestureRecognizerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user