如何计算UITextView中某个位置的NSString的NSRange

发布于 2024-09-14 11:39:45 字数 171 浏览 4 评论 0原文

当我点击 UITextView 中的某个位置时,我想检索该行显示的 NSString 的子字符串。

我有一个 UITextView,它使用 16 行显示一个字符串。当我点击位置 (200, 150) 时,我想要 UITextView 在该点击上显示的子字符串。

关于如何实现这一目标有什么建议吗?

When I tap on a certain position in a UITextView, I want to retrieve the substring of the NSString which is shown at the line.

I have e.g. a UITextView which displays a string, using 16 lines. When I tap on position (200, 150), I want the substring which is shown by UITextView on that tap.

Any suggestions on how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-09-21 11:39:45

如果我正确理解你的问题,这是一个可能的解决方案。在您的视图控制器中将 IBOutlet 添加到 UITextView 并确保您的视图控制器实现 UITextViewDelegate。在 InterfaceBuilder 中将 UITextView 连接到 FileOwner 时,请确保还将 UITextView 的委托指向 FileOwner。

@interface StackOverFlowViewController : UIViewController<UITextViewDelegate> {

    IBOutlet UITextView *textView;
}
@end

然后在您的 UIViewController 实现文件中添加此 UITextViewDelegate 方法。

- (void)textViewDidChangeSelection:(UITextView *)aTextView {

  NSRange rangeOfSelection = textView.selectedRange;
  NSString *selectedText = [textView.text substringWithRange:rangeOfSelection];
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selection Changed" message:selectedText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
  [alert release];
}

然后每次您在 UITextView 中进行选择时都会调用此方法。使用UITextView的selectedRange属性来获取用户创建的NSRange。从那里您可以从 UITextView 获取文本并生成正确的子字符串。

我刚刚在textViewDidChangeSelection方法中添加了一个UIAlertView来显示子字符串

If I understand your question correctly this is a possible solution. In your viewcontroller add an IBOutlet to a UITextView and make sure your viewcontroller implements the UITextViewDelegate. When connecting the UITextView to the FileOwner in InterfaceBuilder make sure you also point the delegate of the UITextView to the FileOwner.

@interface StackOverFlowViewController : UIViewController<UITextViewDelegate> {

    IBOutlet UITextView *textView;
}
@end

Then in your UIViewController implementation file add this UITextViewDelegate Method.

- (void)textViewDidChangeSelection:(UITextView *)aTextView {

  NSRange rangeOfSelection = textView.selectedRange;
  NSString *selectedText = [textView.text substringWithRange:rangeOfSelection];
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selection Changed" message:selectedText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
  [alert release];
}

Then each time you make a selection in the UITextView this method will get called. Use the selectedRange property of UITextView to get the NSRange the User made. From there you can just get the text from the UITextView and generate the correct substring.

I just added a UIAlertView in the textViewDidChangeSelection method to display the substring

谁与争疯 2024-09-21 11:39:45

您必须将 UITextView 设置为可选择和可编辑。但选择时会出现键盘,这会让用户感觉很奇怪。我尝试在 KeyboardDidShow 事件中隐藏键盘(通过 resignFirstResponder),但是 NSRange 将不正确,尤其是当您滚动 UITextView 并选择时。如果你设置UITextView可选择但不可编辑,那么你必须长按才能获取TextViewDidChangeSelection事件,但键盘不会显示,然后NSRange 将是正确的。

后来我找到了一个完美的方法来解决这个问题,将键盘移到较远的位置,这样就不需要长按,只需触摸即可获得准确的 NSRange。这是代码:

  1. 查找如何控制键盘:

    //定义在公共区域
    UIView *systemKeyboard;

    //如果textView可编辑、可选择,触摸textView就会触发该事件。不再使用KeyboardDidShow事件

    -(BOOL)textViewShouldBeginEditing:(UITextView )textView{
    NSLog(@"textViewShouldBeginEditing");
    if(!systemKeyboard){
    UIWindow
    window1 = [UIApplication共享应用].windows[1];
    for (int i = 0; i < window1.subviews.count; i++) {
    UIView *keyBoard1 = window1.subviews[i];
    if ([keyBoard1 isKindOfClass:NSClassFromString(@"UIInputSetContainerView")]) {
    systemKeyboard = keyBoard1;//现在我们可以控制键盘位置
    休息;
    }
    }
    }
    CGRect rec1=systemKeyboard.frame;
    systemKeyboard.frame=CGRectMake(2000、2000、rec1.size.width、rec1.size.height);
    返回是;
    }

  2. 在textViewDidChangeSelection事件中获取NSRange:

    //触摸textView也会触发该事件
    -(void)textViewDidChangeSelection:(UITextView *)textView
    {
    NSLog(@"textViewDidChangeSelection");
    NSRange range=[sermonTextView selectedRange];
    }

  3. 在其他ShouldBeginEditing场合移回键盘:

    //您必须在某处关闭键盘,例如,如果您还有一个将使用键盘的搜索栏,那么您现在应该显示键盘。
    -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"searchBarShouldBeginEditing");
    如果(系统键盘){
    CGRect rec1=systemKeyboard.frame;
    systemKeyboard.frame=CGRectMake(0, 0, rec1.size.width, rec1.size.height);
    // 不需要 [sermonTextView resignFirstResponder];
    // 不需要 [searchBar1成为FirstResponder];
    }
    …………
    返回是;
    }

You must set UITextView both selectable and editable. However the keyboard will show up when you select, which will let user feel strange. I tried to hide keyboard(by resignFirstResponder) in the KeyboardDidShow event, but the NSRange will not be correct especially when you scroll UITextView and select. If you set UITextView selectable but not editable, then you must have a long press to get the TextViewDidChangeSelection event but keyboard will not show up, then NSRange will be correct.

Later I found a perfect way to solve the problem by moving the keyboard to a far location so you don't need long press and can get accurate NSRange by just a touch. Here is the codes:

  1. find how to control the keyboard:

    //define in common area
    UIView *systemKeyboard;

    //touch textView will trigger the event, if textView is editable and selectable. No more use KeyboardDidShow event

    -(BOOL)textViewShouldBeginEditing:(UITextView )textView{
    NSLog(@"textViewShouldBeginEditing");
    if(!systemKeyboard){
    UIWindow
    window1 = [UIApplication sharedApplication].windows[1];
    for (int i = 0; i < window1.subviews.count; i++) {
    UIView *keyBoard1 = window1.subviews[i];
    if ([keyBoard1 isKindOfClass:NSClassFromString(@"UIInputSetContainerView")]) {
    systemKeyboard = keyBoard1;//now we can control keyboard location
    break;
    }
    }
    }
    CGRect rec1=systemKeyboard.frame;
    systemKeyboard.frame=CGRectMake(2000, 2000, rec1.size.width, rec1.size.height);
    return YES;
    }

  2. Get NSRange in textViewDidChangeSelection event:

    //touch textView will trigger the event, too
    -(void)textViewDidChangeSelection:(UITextView *)textView
    {
    NSLog(@"textViewDidChangeSelection");
    NSRange range=[sermonTextView selectedRange];
    }

  3. Move back keyboard in other ShouldBeginEditing occasions:

    //You must close keyboard somewheres, for example if you have also a search bar that will use the keyboard, and you should show the keyboard now.
    -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"searchBarShouldBeginEditing");
    if(systemKeyboard){
    CGRect rec1=systemKeyboard.frame;
    systemKeyboard.frame=CGRectMake(0, 0, rec1.size.width, rec1.size.height);
    // no need [sermonTextView resignFirstResponder];
    // no need [searchBar1 becomeFirstResponder];
    }
    .........
    return YES;
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文