如何修复被角半径覆盖的文本字段

How to fix Text Field which is covered by Corner Radius

我查看了这个答案,很有帮助,但没有得到我想要的:

在属性检查器中,我将 Placeholder 设置为“____ First Name”,它修复了文本的左对齐,因为它向右移动了一点。但我认为这不是正确的做法。另外,我没有完全从上面的答案中得到光标位置的解释。

     func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
        {
            let arbitraryValue: Int = 5
            if let newPosition = textView.position(from: textView.beginningOfDocument, offset: arbitraryValue) {
                textView.selectedTextRange = textView.textRange(from: newPosition, to: newPosition)

            // ....

            }
        }

甚至在我想出办法让它工作之后,光标向右移动了 5 个字符。当我删除文本时,光标移回左角。

但我真正想要的是整个 textField 向右移动,这样它就不会像上面那样被 CornerRadius 覆盖。

所以这是我在没有添加任何代码的情况下所做的。

  • 我将视图放在文本字段下方。
  • 我让视图在两侧都增加了宽度。
  • 我设置了 textFields 的视图颜色。
  • 然后我给他们圆角半径属性.

如果我们仔细观察,我们可以看到那些边缘。他们让我畏缩。

您可以更改文本字段本身的填充。

Swift:

 //Changes the text padding
 override func textRect(forBounds bounds: CGRect) -> CGRect {
   return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 15, 0, 15))
 }
 //Changes the placeholder
 override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
   return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 15, 0, 15))
 }