我有一个UITextView
某些单词被下划线替换为填充空白效果。我在检测这些“空白”上点击时遇到困难。我到目前为止所尝试的是使用rangeEnclosingPosition
的“粒度设置为Word”来获取单词的范围,但看起来它不能识别特殊字符上的点击。现在,我正在寻找给我的'下划线'字符串自定义属性,所以我可以检查,看看是否有任何自定义属性设置。任何想法都会很有帮助。如何检测UITextView下划线的水龙头?
0
A
回答
0
您可以尝试使用UITextViewDelegate方法 - textViewDidChangeSelection通知时插入符号的文本视图的位置发生变化,使你的逻辑在这里如果从插入符号的当前位置的下一个字符是你的特殊字符。
0
以下是我如何做到的 -
将自定义属性添加到文本中的特殊字符。就我而言,我知道特殊字符将全部是下划线,或者这只是我所寻找的。
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:underscoreString attributes:@{ @"yourCustomAttribute" : @"value", NSFontAttributeName : [ UIFont boldSystemFontOfSize:22.0] }];
为了寻找水龙头上的特殊字符,按以下方式添加UITapGestureRecognizer
- -
UITapGestureRecognizer *textViewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedTextView:)];
textViewTapRecognizer.delegate = self;
[self.textView addGestureRecognizer:textViewTapRecognizer];
,并以下列方式确定其选择,所以我通过以下方式添加自定义属性 -
-(void) tappedTextView:(UITapGestureRecognizer *)recognizer
{
UITextView *textView = (UITextView *)recognizer.view;
// Location of the tap in text-container coordinates
NSLayoutManager *layoutManager = textView.layoutManager;
CGPoint location = [recognizer locationInView:textView];
location.x -= textView.textContainerInset.left;
location.y -= textView.textContainerInset.top;
// Find the character that's been tapped on
NSUInteger characterIndex;
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
NSString *value;
if (characterIndex < textView.textStorage.length) {
NSRange range;
value = [[textView.attributedText attribute:@"yourCustomAttribute" atIndex:characterIndex effectiveRange:&range] intValue];
NSLog(@"%@, %lu, %lu", value, (unsigned long)range.location, (unsigned long)range.length);
}
}
如果您得到一个值,您的特殊字符被点击。可以有更好的方法来做到这一点,但现在这对我来说很有效。
相关问题
- 1. 检测曲线上的水龙头?
- 2. ios如何使UITextView检测一个水龙头?
- 3. 检测用户水龙头
- 4. 检测背景水龙头
- 5. UIScrollView检测水龙头
- 6. 检测UITextView中的链接/数字的水龙头
- 7. 设置水龙头的UITextView选择
- 8. 检测UIWebView内的水龙头
- 9. 检测UITableView背景上的水龙头
- 10. Android Widget检测水龙头和双击
- 11. iPhone:在MKMapView中检测水龙头
- 12. 在动画UIImageView上检测水龙头
- 13. 如何检测Palm Pre上的手势区域的水龙头?
- 14. Unity3d如何检测android上的水龙头?
- 15. 如何检测MKPolylines /覆盖像Maps.app上的水龙头?
- 16. 从UITextView中检测地址并在水龙头上显示地图
- 17. 如何检测上表视图用户水龙头
- 18. uitextview内下划线文本
- 19. 检测当前视图外的任何水龙头
- 20. 如何在视图的外侧检测水龙头(视图是小的一个宽度和高度是200)。如何检测视图外侧的水龙头
- 21. UICollectionView:测量单元水龙头的力
- 22. Android下的片段响应水龙头
- 23. 的UITextView水龙头事件来设置可见
- 24. 统一检测球体上的水龙头
- 25. 菜单检测远离其图标的水龙头
- 26. TabBar只检测长时间的水龙头
- 27. iPhone +检测到禁用的水龙头UIControl
- 28. 在uiscrollview中检测uiimageview中的水龙头
- 29. 检测_是否为下划线或下划线
- 30. 如何检测Mac触控板上的四个指状水龙头?
我想检测一个特殊字符的水龙头。键入一个特殊的字符本来就很容易:D – genaks
我会在这里发布我在短时间内做的事:) – genaks