簡體   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