【问题标题】:How to replace Text in UITextInput?如何替换 UITextInput 中的文本?
【发布时间】:2013-03-18 02:42:22
【问题描述】:

我在 customView 中使用 UITextInput:

我的代码:https://github.com/lequysang/github_zip/blob/master/UITextInputDebug.zip

@property (strong,nonatomic) id<UITextInput> textInput;

键入一些字符后,我想返回 4 个字符并替换此位置的文本(将任何替换为 T)。当前光标在末尾。

怎么做?

我尝试使用 UITextRange 替换Range,但不知道如何获取位置和大小!

请帮忙!提前致谢!

【问题讨论】:

  • 在当前光标位置插入文本?对不起,我没有得到你..
  • 我只是找到了 insertText 方法。并且“文本”已经插入。我想要返回 4 个字符并在这个位置替换。
  • 你试过“replaceRange:withText:”吗?
  • 注意:您的textInput 属性应该很弱。强大的代表可能会导致保留周期。

标签: ios objective-c uitextinput


【解决方案1】:
replaceRange:withText:

替换文档中指定范围内的文本。 (必填)

- (void)replaceRange:(UITextRange *)range withText:(NSString *)text

参数

range

文档中的一系列文本。

text

替换范围内文本的字符串。

可用性 适用于 iOS 3.2 及更高版本。

另见

– textInRange: 

声明于UITextInput.h

【讨论】:

  • 再次感谢@Ramshad!你给了我最好的方法!但我坚持使用 UITextRange 和 UITextPosition 获取文本位置。请帮助您了解!
猜你喜欢
  • 2016-01-28
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多