【问题标题】:JSQMessagesViewController custom linkJSQMessagesViewController 自定义链接
【发布时间】:2016-06-30 19:53:23
【问题描述】:

我正在使用 swift framework,并且我需要能够在消息文本字符串中包含 #like 标记。如果用户发送包含以# 为前缀的代码的消息,例如:#t3Us9K,那么单个“单词”需要是所有用户都可以点击的链接。请告诉我这是否可行,如果可以,该怎么做。

【问题讨论】:

  • “我正在使用 swift 框架”指的是什么。 JSQmessages 是 swift 框架还是可以让你检测到 # 的 swift 框架
  • 您的第一个问题的答案是肯定的,这是可能的,但如何可能不适合此表格。
  • @Daniel 我指的框架是 JSQMessagesViewController。你的意思是有多少人不适合这个表格?
  • 您的问题是如何实施整个解决方案,而不是针对单个问题的特定问题。我不是要回答您的问题,但它有点笼统和广泛,如果没有您的整个代码库来查看您正在尝试做什么以及事情是如何设置的,几乎不可能回答这个问题。如果您有一个更具体的问题,那就是堆栈溢出的真正亮点。你不必听我的建议,但由于这都是自愿的,所以我没有人会采取行动。这就是我要说的。

标签: ios swift string jsqmessagesviewcontroller


【解决方案1】:

我一直在研究你的问题,这是我的结果,

首先你需要修改库中的JSQMessagesCellTextView并添加一个方法来帮助你检测自定义链接,比如#test,如果你愿意,你可以克隆我的fork并添加这个功能,这就是它的外观,动画问题是因为我的gif转换器

已编辑

https://github.com/rmelian2014/JSQMessagesViewController/tree/contributing

- (void)detectCustomLinks
{
    NSMutableArray<NSTextCheckingResult*>  *textCheckingResults = [NSMutableArray<NSTextCheckingResult*> array];
    for (NSRegularExpression *exp in [self.regularExpressionsDelegate getRegularExpressions]) {
        NSArray<NSTextCheckingResult*> * matches = [exp matchesInString:self.text options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, self.text.length)];
        [textCheckingResults addObjectsFromArray:matches];
    }

    NSMutableAttributedString * str2 = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];

    [str2 removeAttribute:NSLinkAttributeName range:NSMakeRange(0, str2.string.length)];
    for (NSTextCheckingResult * match in textCheckingResults) {
        [str2 addAttribute: NSLinkAttributeName value:[str2 attributedSubstringFromRange:match.range].string range:match.range];
    }

    self.attributedText = str2;
}

我添加了一个委托来提供正则表达式来检查

@protocol RegularExpressionDelegate <NSObject>

-(NSArray<NSRegularExpression*>*)getRegularExpressions;

@end

/**
 *  `JSQMessagesCellTextView` is a subclass of `UITextView` that is used to display text
 *  in a `JSQMessagesCollectionViewCell`.
 */
@interface JSQMessagesCellTextView : UITextView

@property (weak) id<RegularExpressionDelegate> regularExpressionsDelegate;

@end

然后你需要把你的 viewController 作为 UITextViewDelegate 最后在你的 cellForRowAtIndexPath 你需要把这样的东西放

cell.textView.regularExpressionsDelegate = self;
cell.textView.delegate = self;

这会将你的viewController作为regularExpressionsDelegate,然后你需要实现这个方法,返回你希望被检测为海关链接的正则表达式

- (NSArray<NSRegularExpression*>*)getRegularExpressions
{
    return [NSArray arrayWithObject:[NSRegularExpression regularExpressionWithPattern:@"#([a-zA-Z0-9])+" options:0 error:NULL]];
} 

你也需要实现这个

//UITextViewDelegateMethod
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    //URL param will provide the link that was touched ej:#test
    return YES;
}

希望对你有帮助,问候

【讨论】:

  • 您能否更新您的答案以说明如何实施新版本?
  • OK @Jacolack 是对的,我将解释现在是如何实现的
  • 如果可能的话,你能在那里回答我的问题stackoverflow.com/q/38581701/6481734,我需要在 swift 中使用一种方法
  • 不,我需要它在 swift... 只是 nsregularexpression nsarray 方法... 或者这就是你的意思?斯威夫特?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多