长按

时间:2016-06-16 18:55:59

标签: ios uitextview textcolor jsqmessagesviewcontroller

我正在使用JSQMessagesViewController我注意到我长按一个外出单元格,如果它有某些文字,例如美元金额或日期,则长按时文字颜色会变为黑色。 / p>

这是一个前后截图(我长按第一个单元格):

enter image description here

你可以看到“1,300美元”神秘地变黑了。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我一直在审核图书馆JSQMessagesViewController的代码,这是我的结果

首先,您需要在.h

中添加JSQMessagesCellTextView
@property (nonatomic,strong) UIColor * originalTextColor;

然后在.m

添加此方法

- (BOOL)haveValidLinks
{
    NSError *error = nil;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress
                                | NSTextCheckingTypePhoneNumber | NSTextCheckingTypeDate
                                                               error:&error];

    NSInteger number = [detector numberOfMatchesInString:self.text options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, self.text.length)];

    if(number > 0)
        return YES;

    return NO;
}

然后

替换

上的现有代码

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

用这个

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{

    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        if(![self haveValidLinks])
        {
        self.dataDetectorTypes = UIDataDetectorTypeNone;
        self.textColor = self.originalTextColor;
        }
    }
    self.dataDetectorTypes = UIDataDetectorTypeAll;
    //  ignore double-tap to prevent copy/define/etc. menu from showing
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *tap = (UITapGestureRecognizer *)gestureRecognizer;
        if (tap.numberOfTapsRequired == 2) {
            return NO;
        }
    }

    return YES;
}

然后您需要修改- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

喜欢这个

- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     *  Override point for customizing cells
     */
    JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];

    /**
     *  Configure almost *anything* on the cell
     *
     *  Text colors, label text, label colors, etc.
     *
     *
     *  DO NOT set `cell.textView.font` !
     *  Instead, you need to set `self.collectionView.collectionViewLayout.messageBubbleFont` to the font you want in `viewDidLoad`
     *
     *
     *  DO NOT manipulate cell layout information!
     *  Instead, override the properties you want on `self.collectionView.collectionViewLayout` from `viewDidLoad`
     */

    JSQMessage *msg = [self.demoData.messages objectAtIndex:indexPath.item];

    if (!msg.isMediaMessage) {

        if ([msg.senderId isEqualToString:self.senderId]) {
            cell.textView.textColor = [UIColor whiteColor];
            cell.textView.originalTextColor = [UIColor whiteColor];
        }
        else {
            cell.textView.textColor = [UIColor blackColor];
            cell.textView.originalTextColor = [UIColor blackColor];
        }

        cell.textView.linkTextAttributes = @{ NSForegroundColorAttributeName : cell.textView.textColor,
                                              NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) };
    }

    return cell;
}

代码在这里

https://github.com/rmelian2014/JSQMessagesViewController

我希望这有助于你