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

时间:2018-09-21 10:15:01

标签: swift xcode xcode10 swift4.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 Update之后会给出错误。这是一个错误:

'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
}

但是上面的代码不会影响到文本字段。我应该写些什么来从左开始填充?

1 个答案:

答案 0 :(得分:1)

返回此:

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

我刚刚尝试将MyTextField自定义类放到情节提要中,它似乎确实起作用。

将您的问题发表评论:

您将在这里看到textRect documentation editRect返回可以在其中显示可编辑文本的矩形,而不是textRect返回所计算的图形rect。检查上一个链接,它将更加详尽。