【问题标题】:Move cursor in uitextfield 3 space to the right将 uitextfield 3 空间中的光标向右移动
【发布时间】:2021-03-15 12:51:33
【问题描述】:

当我单击 uitextfield 时,如何在 uitextfield 中向右移动和显示光标 3 个空格。我将 uitextfield 设为圆角并将约束从 main.storyboard 向左“10”放置,当我单击 uitextfield 时,光标显示在 uitextfield 之外。下面是快速代码。

@IBOutlet weak var txtSearch: UITextField!
override func viewDidLoad() {
        super.viewDidLoad()
        txtSearch.layer.cornerRadius = 22
        txtSearch.clipsToBounds = true
        txtSearch.layer.borderWidth = 2.0
        txtSearch.borderStyle = .none
}

【问题讨论】:

    标签: ios swift uitextfield


    【解决方案1】:

    要实现这一点,您需要通过子类化标准对象然后覆盖设置内容边界(即插入)的函数来创建自定义 UITextField。您需要以点而非字符为单位工作,因此请使用插入变量来获得您想要的外观。

    class MyTextField: UITextField {
       var insetX: CGFloat = 20
       var insetY: CGFloat = 0
       
       override func textRect(forBounds bounds: CGRect) -> CGRect {
          return bounds.insetBy(dx: insetX , dy: insetY)
       }
       
       override func editingRect(forBounds bounds: CGRect) -> CGRect {
          return bounds.insetBy(dx: insetX , dy: insetY)
       }
    }
    

    您需要将 IB 中的文本字段的类设置为您的自定义类。

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2015-11-13
      • 2021-09-15
      相关资源
      最近更新 更多