iphone - 如何从 UITextView 获取选定的文本框

标签 iphone ios cocoa-touch ipad uipopovercontroller

我正在尝试从 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)};

编辑:由于示例代码变得有点难以获得,我添加了一张图片作为补充。

An image that illustrates what local variables represent

关于iphone - 如何从 UITextView 获取选定的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683848/

相关文章:

objective-c - 何时使用静态字符串与 #define

iPhone SDK通过wifi发送字符串

ios - 如何在 IOS 8 中使 UINavigationBar 透明?

iphone - HTML 未正确加载

iphone - 如何让应用程序显示在应用程序加载器中?

objective-c - 同步操作(例如 stringFromURL)是否会被其他线程抢占?

html - 在iPhone中,键盘弹出时光标会停留在原来的位置

ios - 从文件路径加载视频

ios - iOS Swift XMPPFramework,如何强制认证机制?

ios - 在 Storyboard中使用 Swift 嵌套类作为 UIViewController