【问题标题】:set the padding of a text field using swift in xcode在 xcode 中使用 swift 设置文本字段的填充
【发布时间】:2019-04-14 19:20:53
【问题描述】:

如何使用 swift 在文本字段的开头创建一些空格? 我看了帖子

padding of text field

我不明白子类在做什么以及如何使用该类进行填充。

非常感谢

【问题讨论】:

标签: ios swift


【解决方案1】:

https://stackoverflow.com/a/27066764/846780

这个答案很明确,

  1. 如果您继承 UITextField,您可以覆盖 textRectForBoundseditingRectForBoundsplaceholderRectForBounds。这些方法只允许您向文本字段的标签添加一个矩形(框架)。

  2. newBounds 方法创建将添加到文本字段标签的矩形(框架)。

  3. 最后你的填充是:let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);

  4. 现在您有了一个具有自定义填充的自定义 UITextField。

  5. 例如,如果您像 class MyTextField: UITextField 这样创建新的子类,您只需更改已添加到 IB 文件中的 UITextField 的类。

【讨论】:

  • 非常感谢!我忘记将我的文本字段的类更改为新的子类。填充设置为现在的默认填充,即(顶部:0,左侧:5,底部:0,右侧:5)。如果我想修改填充而不每次都编辑子类怎么办?我将此添加到 viewController "myTextField.textRectForBounds(CGRect(x: 20, y: 0, width: 0, height: 0))" 因为我想从左侧获得更多填充。但什么都没有改变。你能帮我解决这个问题吗?
【解决方案2】:

补充 klevison-matias 的答案,这是我想在 Swift 3 中向我的 TextField 添加填充时使用的代码

//UITextField : override textRect, editingRect 
class LeftPaddedTextField: UITextField {

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height)
    }

}

然后在我的 TextField 中我以这种方式使用:

let emailTextField: LeftPaddedTextField = {
    let textField = LeftPaddedTextField()
    textField.placeholder = "Enter email"
    textField.layer.borderColor = UIColor.lightGray.cgColor
    textField.layer.borderWidth = 1
    textField.keyboardType = .emailAddress
    return textField
}()

let passwordTextField: LeftPaddedTextField = {
    let textField = LeftPaddedTextField()
    textField.placeholder = "Enter password"
    textField.layer.borderColor = UIColor.lightGray.cgColor
    textField.layer.borderWidth = 1
    textField.isSecureTextEntry = true
    return textField
}()

【讨论】:

  • 你不需要从返回的 CGRect 的宽度中减去你的 insets 吗?否则,您的文本将阻止 clearTextIndicator 或 UITextField 中包含的其他视图。
【解决方案3】:

创建 TextField 插座并使用以下代码。说“nameTextField”必须添加填充。

let paddingView = UIView(frame: CGRectMake(x: 0, y: 0, width: 30, height: 30))
nameTextField.leftView = paddingView
nameTextField.leftViewMode = .always
nameTextField.placeholder = "Enter Name"

您可以在 paddingView 中添加 Image。

【讨论】:

    【解决方案4】:

    根据@Jorge Casariego 的回答,我想出了这个解决方案。

    您可以通过 Interface Builder 设置 PaddedTextField 类和所需的填充!

    class PaddedTextField: UITextField {
    
        @IBInspectable var padding: CGFloat = 0
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
        }
    }
    

    【讨论】:

    • 终于!当您的文本字段有清除按钮时效果很好。
    【解决方案5】:

    完整的代码示例: 确保您的 TextField 通过故事板指向 RoundedTextField

    import UIKit
    
    class RoundedTextField: UITextField {
    
        override func awakeFromNib() {
            // add rounded corner
            self.layer.cornerRadius = 15.0
            self.clipsToBounds = false
    
            super.awakeFromNib()
        }
    
        // For the padding from the left
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height)
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2016-01-31
      • 1970-01-01
      • 2011-02-21
      • 2021-11-10
      相关资源
      最近更新 更多