new way for ChatTextView detect mention

This commit is contained in:
nixzhu
2016-01-13 15:55:02 +08:00
parent ac3a8e318f
commit eec8e424ce
3 changed files with 102 additions and 0 deletions
@@ -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)
+98
View File
@@ -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 Reveal0.12 tap link0.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 {
}
}
*/