如何使UITextView文本可单击

时间:2017-04-02 05:46:43

标签: ios objective-c uitextview 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);

感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

当字符串被分配到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;
}

您可以获得所需的点击值。

相关问题