繁体   English   中英

更改UITextfield输入的宽度

[英]Change Width of UITextfield Input

有没有办法更改我的UITextield的输入大小,使其不与我的“ vergessen”按钮重叠?

Textfield是一个简单的TextfieldView,宽度为315。我添加了“ Vergessen?”。 以编程方式按下下面的代码。

func createForgetButton () {
    let button = UIButton(type: .system)
    button.setTitle("Vergessen?", for: .normal)
    button.addTarget(self, action: #selector(self.vergessenTapped(_:)), for: .touchUpInside)
    button.titleLabel?.font = UIFont(name: "Avenir Next", size: 19.0)
    passwordTextField.rightView = button
    passwordTextField.rightViewMode = .unlessEditing
}

在此处输入图片说明

使用以下自定义类的字段。 然后,更新IB或代码中的.rightPadding字段,以匹配右侧按钮的宽度。

/**
 * Text field with some changes according to design
 *
 * - author: Alexander Volkov
 * - version: 1.0
 */
@IBDesignable public class CustomTextField: UITextField {

    /// the left padding
    @IBInspectable public var leftPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }

    /// the right padding
    @IBInspectable public var rightPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }

    /// Text rectangle
    ///
    /// - Parameter bounds: the bounds
    /// - Returns: the rectangle
    override public func textRect(forBounds bounds: CGRect) -> CGRect {
        let originalRect: CGRect = super.editingRect(forBounds: bounds)
        return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
    }

    /// Editing rectangle
    ///
    /// - Parameter bounds: the bounds
    /// - Returns: the rectangle
    override public func editingRect(forBounds bounds: CGRect) -> CGRect {
        let originalRect: CGRect = super.editingRect(forBounds: bounds)
        return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM