gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:09:12 26 4
gpt4 key购买 nike

我正在尝试从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com