gpt4 book ai didi

ios - 长按时某些单词的文本字段的文本颜色会发生变化

转载 作者:行者123 更新时间:2023-11-29 00:53:27 25 4
gpt4 key购买 nike

我正在使用JSQMessagesViewController,我注意到我长按一个传出单元格,如果它有某些文本,例如美元金额或日期,长按时文本颜色将变为黑色按下。

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

enter image description here

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

最佳答案

我一直在审查库 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

希望对你有帮助

关于ios - 长按时某些单词的文本字段的文本颜色会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37867297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com