UITextField、textRect、Xcode 版本 10.0、部署目标 12.0、Swift 4.2

UITextField, textRect, Xcode Version 10.0, Deployment Target 12.0, Swift 4.2

我之前使用过这个代码范围:

override func textRect(forBounds bounds: CGRect) -> CGRect {
    super.textRect(forBounds: bounds)
    return UIEdgeInsetsInsetRect(bounds, UIEdgeInsets(top: 0,left: 10, bottom: 0, right: 10))
}

但是Xcode、Swift更新后报错。这是一个错误:

'UIEdgeInsetsInsetRect' has been replaced by instance method 'CGRect.inset(by:)'

然后我写了这个:

override func textRect(forBounds bounds: CGRect) -> CGRect {
    super.textRect(forBounds: bounds)
    var animationRect = frame.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10))
    return animationRect
}

但是上面的代码不影响进入文本域。我应该写什么来从左边填充?

return 这个:

override func textRect(forBounds bounds: CGRect) -> CGRect { 
   return bounds.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10)) 
} 

我刚刚尝试将 MyTextField 自定义 class 放入情节提要中,它似乎确实有效。

在评论中提出你的问题:

正如您在此处看到的那样 textRect documentation editingRect return 是可以显示可编辑文本的矩形,而不是 textRect return 计算出的绘图矩形。查之前的link,会更详尽。