【问题标题】:How to change the starting cursor position inside UITextView如何更改 UITextView 内的起始光标位置
【发布时间】:2015-06-20 09:27:54
【问题描述】:

我有一个 uitextview,它成为 viewload 的第一响应者。但我只想将 uitextview 光标的位置更改为 x 中某个位置的前 2 行。

【问题讨论】:

  • 来自谷歌的 uitextview 的主要委托方法,但没有工作 @d4Rk
  • 你能给我们看一些代码吗,什么不起作用?或者你期望它做什么。
  • 我只想像 facebook 屏幕一样在 android 中更新状态

标签: ios iphone ios8 uitextview uitextviewdelegate


【解决方案1】:

也许您可以使用 UITextKit (a very good tutorial)。

例如,要使用圆形文本,您可以使用以下内容:

UIBezierPath* exclusionPath = [UIBezierPath bezierPathWithOvalInRect:yourTextView.bounds];
exclusionPath = [exclusionPath bezierPathByReversingPath];
yourTextView.textContainer.exclusionPaths = @[exclusionPath];

【讨论】:

    【解决方案2】:

    您是否尝试过以下解决方案?

    在 UITextField 中控制光标位置很复杂,因为输入框和计算位置涉及很多抽象。但是,这当然是可能的。可以使用成员函数setSelectedTextRange

    [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    

    这是一个函数,它接受一个范围并选择该范围内的文本。如果您只想将光标放在某个索引处,只需使用长度为 0 的范围:

    + (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
        UITextPosition *start = [input positionFromPosition:[input beginningOfDocument] 
                                                     offset:range.location];
        UITextPosition *end = [input positionFromPosition:start
                                                   offset:range.length];
        [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    }
    

    例如,将光标放在 UITextField 中的idx input

        [Helpers selectTextForInput:input 
                            atRange:NSMakeRange(idx, 0)];
    

    来源:https://stackoverflow.com/a/11532718/914111

    由于有些忙,没有测试,所以请告诉我们它是否工作(可能在 IOS8.0 中存在问题)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 2018-09-11
      • 2012-07-16
      • 2011-02-07
      相关资源
      最近更新 更多