我正在尝试从 UITextView
中选定文本的矩形显示 UIPopoverController
,如何获取选定文本 CGRect
?
谢谢!
最佳答案
我认为 [UITextInput selectedTextRange]
和 [UITextInput caretRectForPosition:]
是您要查找的内容。
[UITextInput selectedTextRange]
以字符返回所选范围
[UITextInput caretRectForPosition:]
返回此输入中字符范围的 CGRect
。
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)};
编辑:由于示例代码变得有点难以获得,我添加了一张图片作为补充。
关于iphone - 如何从 UITextView 获取选定的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683848/