gpt4 book ai didi

ios - 在 UITextView 中检测点击的字符串

转载 作者:可可西里 更新时间:2023-11-01 06:20:06 27 4
gpt4 key购买 nike

我试图在 UITextView 中找到 Tapped String。我试过 UIDataDetectorTypeLink。但它不起作用。我也不确定是否可以检测到 String。我在自定义 UITableViewCell 中使用 UITextView。从那我试图检测被窃听的字符串。下面也是我尝试过的示例代码。谁能帮我找出我做错了什么,我该如何解决?即使不可能,我该如何解决这个问题?

NSMutableArray *sampleArray = [[NSMutableArray alloc]init];
NSMutableString *mutableString = [[NSMutableString alloc]init];
NSMutableAttributedString * string;
sampleArray = [[postArray valueForKey:@"hash_tags"] objectAtIndex:indexPath.row];
for(NSString *sampleString in sampleArray){
if (mutableString.length == 0) {
[mutableString appendString:sampleString];
}else{
[mutableString appendString:[NSString stringWithFormat:@", %@",sampleString]];
}
string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",mutableString]];
// [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,[string length])];
}
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:5.0f/255.0f green:107.0f/255.0f blue:153.0f/255.0f alpha:1.0f], NSUnderlineColorAttributeName: [UIColor greenColor],NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
[string addAttribute:NSLinkAttributeName
value:string
range:NSMakeRange(0, [string length])];//[[string string] rangeOfString:sampleString]];
[cell.tagsTextView setAttributedText:string];
[cell.tagsTextView setDelegate:self];
[cell.tagsTextView setUserInteractionEnabled:YES];
cell.tagsTextView.scrollEnabled = NO;
cell.tagsTextView.editable = NO;
cell.tagsTextView.dataDetectorTypes = UIDataDetectorTypeLink;
// cell.tagsTextView.textContainer.lineFragmentPadding = 0;
// cell.tagsTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
[cell.tagsTextView setLinkTextAttributes:linkAttributes];

UITextView 委托(delegate)

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
NSLog(@"url %@",URL);
if ([[URL scheme] isEqualToString:@"username"]) {
NSString *username = [URL host];
// do something with this username
// ...
return NO;
}
return YES; // let the system open this URL
}

提前致谢

最佳答案

已将 UITapGestureRecognizer 添加到 UITextView 下方是 UITapGestureRecognizer选择器方法

- (void) tapResponse:(UITapGestureRecognizer *)recognizer{

UITextView *textView = (UITextView *)recognizer.view;
CGPoint location = [recognizer locationInView:textView];
NSLog(@"Tap Gesture Coordinates: %.2f %.2f -- %@", location.x, location.y,textView.text);

CGPoint position = CGPointMake(location.x, location.y);
//get location in text from textposition at point
UITextPosition *tapPosition = [textView closestPositionToPoint:position];

//fetch the word at this position (or nil, if not available)
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString *tappedSentence = [textView textInRange:textRange];//[self lineAtPosition:CGPointMake(location.x, location.y)];
NSLog(@"selected :%@ -- %@",tappedSentence,tapPosition);
}

关于ios - 在 UITextView 中检测点击的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635628/

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