【问题标题】:UITextView get selected text pointUITextView 获取选定的文本点
【发布时间】:2011-12-30 14:23:16
【问题描述】:

我想知道有什么方法可以在 UITextView 中获取所选文本的重点?

谢谢!

【问题讨论】:

  • 点是什么意思?你的意思是 x & y 坐标?
  • 你真的不能用苹果定义的方法来做到这一点(除非你解决它,使用光标所在的点......)你想完成什么?
  • 我有一个 UIMenuController,我想在点击 UIMenuController 后在所选单词中显示一个 UIPopoverController

标签: iphone objective-c ios uitextview


【解决方案1】:

对于 iOS5 及更高版本:现在 UITextFieldUITextView 符合 UITextInput 协议,所以它是可能的:)

选择插入符号前的最后 5 个字符如下所示:

//Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];

NSLog("Start: %d <> End: %d", selectedRange.start, selectedRange.end);

//Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5];

//Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];

【讨论】:

  • 我认为这个问题的意思是,它被点击的位置的 CGPointMake(x,y) 坐标。
【解决方案2】:

是的,首先像这样获取活动选择:myTextView.selectedRange,然后将其传递给此方法[myTextView firstRectForRange:range] 这将为您提供第一个边界矩形(如果它是一条线,则为整个选择,以及如果是多行,则选择第一行)作为CGRect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    相关资源
    最近更新 更多