如何使 UITextView 文本可点击
How to make UITextView Text Clickable
我有几个字符串由 /
连接并放在 UITextview
中。我必须让 /
之前的每个字符串都可点击。我不知道具体的字符串或其范围。我使用点击手势识别器给我触摸的词,但我想要整个字符串。例如,我的字符串是 "ABC-1234/DEF-1234/ghi-9874"。但是当我点击它时,我得到 "abc" 或 "ghi"。如何获取整个字符串?
下面是我当前的代码,它没有按预期工作:
UITextView *textView = (UITextView *)tapRecognizer.view;
CGPoint location = [tapRecognizer locationInView:textView];
CGPoint position = CGPointMake(location.x, location.y);
UITextPosition *tapPosition = [textView closestPositionToPoint:position];
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
// NSString *tappedWord = [textView textInRange:textRange];
我尝试使用 NSScanner
但我也没有成功。下面是使用 NSScanner
的代码。它只给我最后一个值:
NSString *plainString = self.SelectedTextView.attributedText.string;
NSMutableArray* substrings = [[NSMutableArray alloc]init];
NSScanner *scanner = [[NSScanner alloc]initWithString:plainString];
[scanner scanUpToString:@"/" intoString:nil];
while (![scanner isAtEnd]) {
NSString* substring = nil;
[scanner scanString:@"/" intoString:nil];
NSString* space = @" ";
if ([scanner scanUpToString:space intoString:&substring]) {
[substrings addObject:substring];
}
[scanner scanUpToString:@"/" intoString:nil];
}
NSLog(@"%@",substrings);
感谢任何帮助。提前致谢。
当时将字符串分配给 textview 时,我调用了一种方法来计算所选 textview 中的元素
-(void)ElementsInSelectedTextView
{
localizedStringPieces = [NSArray new];
localizedStringPieces = [_SelectedTextView.text componentsSeparatedByString:@"/"];
arrMinMax = [[NSMutableArray alloc] init];
NSUInteger count = 0;
for (int i = 0; i <[localizedStringPieces count]; i++) {
NSString * str =[localizedStringPieces objectAtIndex:i];
NSUInteger length = count + str.length-1;
NSMutableDictionary * tempdic = [[NSMutableDictionary alloc]init];
[tempdic setObject:[NSNumber numberWithUnsignedInteger:count] forKey:@"MinValue"];
[tempdic setObject:[NSNumber numberWithUnsignedInteger:length] forKey:@"MaxValue"];
[arrMinMax addObject:tempdic];
count = length +1;
}
}
在textView上添加点击手势(这里_selectedtextview是我的uitextview对象名)
UITapGestureRecognizer *lblTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SelectedProductTextViewDetected:)];
[_SelectedTextView addGestureRecognizer:lblTapRecognizer];
// prevents the scroll view from swallowing up the touch event of child buttons
lblTapRecognizer.cancelsTouchesInView = NO;
在选择器方法中添加以下代码
- (void)SelectedProductTextViewDetected:(UITapGestureRecognizer *)tapRecognizer
{
NSUInteger characterIndex;
NSLayoutManager *layoutManager = _SelectedTextView.layoutManager;
CGPoint location = [tapRecognizer locationInView:_SelectedTextView];
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:_SelectedTextView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
NSString * strValue;
for (int j = 0; j<[arrMinMax count]; j++) {
if (characterIndex >=[[[arrMinMax objectAtIndex:j] objectForKey:@"MinValue"] integerValue] && characterIndex <=[[[arrMinMax objectAtIndex:j] objectForKey:@"MaxValue"] integerValue]) {
strValue = [localizedStringPieces objectAtIndex:j];
SelectedProductIndex = j;
}
}
_SelectedTextView.text = strValue;
[self.SelectedTextView layoutIfNeeded];
_SelectedTextView.editable = NO;
}
您获得了想要的点击值。
我有几个字符串由 /
连接并放在 UITextview
中。我必须让 /
之前的每个字符串都可点击。我不知道具体的字符串或其范围。我使用点击手势识别器给我触摸的词,但我想要整个字符串。例如,我的字符串是 "ABC-1234/DEF-1234/ghi-9874"。但是当我点击它时,我得到 "abc" 或 "ghi"。如何获取整个字符串?
下面是我当前的代码,它没有按预期工作:
UITextView *textView = (UITextView *)tapRecognizer.view;
CGPoint location = [tapRecognizer locationInView:textView];
CGPoint position = CGPointMake(location.x, location.y);
UITextPosition *tapPosition = [textView closestPositionToPoint:position];
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
// NSString *tappedWord = [textView textInRange:textRange];
我尝试使用 NSScanner
但我也没有成功。下面是使用 NSScanner
的代码。它只给我最后一个值:
NSString *plainString = self.SelectedTextView.attributedText.string;
NSMutableArray* substrings = [[NSMutableArray alloc]init];
NSScanner *scanner = [[NSScanner alloc]initWithString:plainString];
[scanner scanUpToString:@"/" intoString:nil];
while (![scanner isAtEnd]) {
NSString* substring = nil;
[scanner scanString:@"/" intoString:nil];
NSString* space = @" ";
if ([scanner scanUpToString:space intoString:&substring]) {
[substrings addObject:substring];
}
[scanner scanUpToString:@"/" intoString:nil];
}
NSLog(@"%@",substrings);
感谢任何帮助。提前致谢。
当时将字符串分配给 textview 时,我调用了一种方法来计算所选 textview 中的元素
-(void)ElementsInSelectedTextView
{
localizedStringPieces = [NSArray new];
localizedStringPieces = [_SelectedTextView.text componentsSeparatedByString:@"/"];
arrMinMax = [[NSMutableArray alloc] init];
NSUInteger count = 0;
for (int i = 0; i <[localizedStringPieces count]; i++) {
NSString * str =[localizedStringPieces objectAtIndex:i];
NSUInteger length = count + str.length-1;
NSMutableDictionary * tempdic = [[NSMutableDictionary alloc]init];
[tempdic setObject:[NSNumber numberWithUnsignedInteger:count] forKey:@"MinValue"];
[tempdic setObject:[NSNumber numberWithUnsignedInteger:length] forKey:@"MaxValue"];
[arrMinMax addObject:tempdic];
count = length +1;
}
}
在textView上添加点击手势(这里_selectedtextview是我的uitextview对象名)
UITapGestureRecognizer *lblTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SelectedProductTextViewDetected:)];
[_SelectedTextView addGestureRecognizer:lblTapRecognizer];
// prevents the scroll view from swallowing up the touch event of child buttons
lblTapRecognizer.cancelsTouchesInView = NO;
在选择器方法中添加以下代码
- (void)SelectedProductTextViewDetected:(UITapGestureRecognizer *)tapRecognizer
{
NSUInteger characterIndex;
NSLayoutManager *layoutManager = _SelectedTextView.layoutManager;
CGPoint location = [tapRecognizer locationInView:_SelectedTextView];
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:_SelectedTextView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
NSString * strValue;
for (int j = 0; j<[arrMinMax count]; j++) {
if (characterIndex >=[[[arrMinMax objectAtIndex:j] objectForKey:@"MinValue"] integerValue] && characterIndex <=[[[arrMinMax objectAtIndex:j] objectForKey:@"MaxValue"] integerValue]) {
strValue = [localizedStringPieces objectAtIndex:j];
SelectedProductIndex = j;
}
}
_SelectedTextView.text = strValue;
[self.SelectedTextView layoutIfNeeded];
_SelectedTextView.editable = NO;
}
您获得了想要的点击值。