Ios 如何使UITextView文本可单击
我有几个字符串由Ios 如何使UITextView文本可单击,ios,objective-c,uitextview,clickable,Ios,Objective C,Uitextview,Clickable,我有几个字符串由/连接,并放置在UITextview中。我必须使/之前的每个字符串都可单击。我不知道具体的字符串或它的范围。我用轻拍手势识别器给我触摸过的单词,但我想要整个字符串。例如,我的字符串是“ABC-1234/DEF-1234/ghi-9874”。但当我点击它时,我要么得到“abc”要么得到“ghi”。如何获得整个字符串 以下是我当前的代码,它不能按预期工作: UITextView *textView = (UITextView *)tapRecognizer.view; CGPoin
/
连接,并放置在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;
}
}
在选择器方法中添加以下代码
- (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;
}
-(void)SelectedProductTextViewDetected:(UITapgestureRecognitor*)点击识别器
{
NSU整数特征索引;
NSLayoutManager*layoutManager=\u选择的TEXTVIEW.layoutManager;
CGPoint location=[点击识别器位置查看:_SelectedTextView];
characterIndex=[layoutManager CharacterIndexorPoint:位置
inTextContainer:\u选择了TextView.textContainer
插入点之间距离的分位数:NULL];
NSString*标准值;
对于(int j=0;j=[[arrMinMax objectAtIndex:j]objectForKey:@“MinValue”]integerValue]&&characterIndex,您解决了这个问题吗?是的,我解决了,等我将答案发布到@Abdul Jamil
- (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;
}