ios – 在UITextField的光标位置插入字符串

前端之家收集整理的这篇文章主要介绍了ios – 在UITextField的光标位置插入字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在UITableView中有几个UITextField.用户应该只能插入数字和点.为此,我将键盘类型设置为UIKeyboardTypeNumberPad,并在左下角添加了一个“.”按钮.每按一次按钮,就会调用一个功能.此函数应该在当前光标位置插入一个点,但这是一个问题:UITextField没有一个selectedRange属性,所以我无法获取当前的光标位置.有没有人知道如何解决这个问题,还是有其他的方法呢?谢谢.

解决方法

我终于找到了解决这个问题的解决方案!您可以将需要插入的文本放入系统粘贴板,然后将其粘贴到当前光标位置:
[myTextField paste:self]

我在这个人的博客中找到了解决方案:
@L_301_1@

粘贴功能是操作系统V3.0具体的,但是我已经测试了它,它对我来说适用于自定义键盘.

更新:根据Jasarien在下面的评论,最好先把它保存下来,然后再恢复.为方便起见,这里是我的代码

// Get a reference to the system pasteboard
  UIPasteboard* lPasteBoard = [UIPasteboard generalPasteboard];

  // Save the current pasteboard contents so we can restore them later
  NSArray* lPasteBoardItems = [lPasteBoard.items copy];

  // Update the system pasteboard with my string
  lPasteBoard.string = @"-";

  // Paste the pasteboard contents at current cursor location
  [myUIField paste:self];

  // Restore original pasteboard contents
  lPasteBoard.items = lPasteBoardItems;

  [lPasteBoardItems release];
原文链接:https://www.f2er.com/iOS/336326.html

猜你在找的iOS相关文章