【问题标题】:How to fix Text Field which is covered by Corner Radius如何修复角半径覆盖的文本字段
【发布时间】:2018-09-07 21:34:16
【问题描述】:

我查看了这个答案,很有帮助,但没有得到我想要的:Getting and Setting Cursor Position of UITextField and UITextView in Swift

在属性检查器中,我将占位符设置为“____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 覆盖。

【问题讨论】:

    标签: ios swift3 uitextfield


    【解决方案1】:

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

    斯威夫特:

     //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))
     }
    

    【讨论】:

    • 为什么要对函数使用覆盖?它会给你错误“方法不会覆盖其超类中的任何方法”。
    【解决方案2】:

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

    • 我将视图放置在 textField 下方。
    • 我让视图在两边都增加了宽度。
    • 我将 Views 颜色设置为 textFields 的颜色。
    • 然后我给他们圆角半径属性。

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

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多