本文介绍了如何从 UITextView 获取选定的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 UITextView 中选定文本的矩形显示 UIPopoverController,如何获取选定文本 CGRect> ?

I'm trying to display an UIPopoverController from the rect of a selected text in an UITextView, How can I get the selected text CGRect ?

谢谢!

推荐答案

我认为 [UITextInput selectedTextRange][UITextInput caretRectForPosition:] 是您正在寻找的.

I think [UITextInput selectedTextRange] and [UITextInput caretRectForPosition:] is what you are looking for.

[UITextInput selectedTextRange] 返回选中的字符范围

[UITextInput caretRectForPosition:] 返回此输入中字符范围的 CGRect.

[UITextInput caretRectForPosition:] returns the CGRect of the character range in this input.

UITextView 符合 UITextInput(自 iOS 5 起),因此您可以将这些方法用于 UITextView 实例.

它会是这样的.

UITextRange * selectionRange = [textView selectedTextRange];
CGRect selectionStartRect = [textView caretRectForPosition:selectionRange.start];
CGRect selectionEndRect = [textView caretRectForPosition:selectionRange.end];
CGPoint selectionCenterPoint = (CGPoint){(selectionStartRect.origin.x + selectionEndRect.origin.x)/2,(selectionStartRect.origin.y + selectionStartRect.size.height / 2)};

由于示例代码变得有点难以获得,我添加了一个图像作为补充.

EDIT : Since the sample code became a little hard to get, I added an image for complementing.

这篇关于如何从 UITextView 获取选定的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:10