簡體   English   中英

如何在 SkyFloatingLabelTextField 中垂直和水平對齊文本?

[英]How to vertically and horizontally align text in SkyFloatingLabelTextField?

我使用了 SkyFloatingLabelTextField ,我想垂直和水平居中對齊文本。

我必須編寫代碼,但它只水平對齊文本而不是垂直對齊。

我試過的是

        var rightButton = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
        var imageNew = UIImage(named: "rightArrow")!
        rightButton.contentMode = .scaleAspectFit
        rightButton.setImage(imageNew, for: .normal)
        rightButton.setImage(imageNew, for: .selected)
        
        rightButton.contentVerticalAlignment = .center
        rightButton.contentHorizontalAlignment = .center


        selectTradeTxt.rightView = rightButton
        selectTradeTxt.rightView?.frame = self.selectTradeTxt.rightViewRect(forBounds: self.selectTradeTxt.bounds)
        
        selectTradeTxt.rightViewMode = .always
        selectTradeTxt.textColor = .white
        selectTradeTxt.addRoundCorner(7)
// i have tried this
  //      selectTradeTxt.contentHorizontalAlignment = .center
        selectTradeTxt.contentVerticalAlignment = .center
        selectTradeTxt.textAlignment = .center

請選擇您的行業和技能

您必須在 SkyFloatingLabelTextField.swift 文件中將框架 y position 更改為波紋管覆蓋 function

// MARK: - UITextField text/placeholder positioning overrides
    /**
    Calculate the rectangle for the textfield when it is not being edited
    - parameter bounds: The current bounds of the field
    - returns: The rectangle that the textfield should render in
    */
    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        let superRect = super.textRect(forBounds: bounds)
        let titleHeight = self.titleHeight()

        let rect = CGRect(
            x: superRect.origin.x,
            y: titleHeight + 2,
            width: superRect.size.width,
            height: superRect.size.height - titleHeight - selectedLineHeight
        )
        return rect
    }

    /**
     Calculate the rectangle for the textfield when it is being edited
     - parameter bounds: The current bounds of the field
     - returns: The rectangle that the textfield should render in
     */
    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        let superRect = super.editingRect(forBounds: bounds)
        let titleHeight = self.titleHeight()

        let rect = CGRect(
            x: superRect.origin.x,
            y: titleHeight + 2,
            width: superRect.size.width,
            height: superRect.size.height - titleHeight - selectedLineHeight
        )
        return rect
    }

    /**
     Calculate the rectangle for the placeholder
     - parameter bounds: The current bounds of the placeholder
     - returns: The rectangle that the placeholder should render in
     */
    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        let rect = CGRect(
            x: 0,
            y: self.isEditing ? titleHeight() : titleHeight() - 8,
            width: bounds.size.width,
            height: bounds.size.height - titleHeight() - selectedLineHeight
        )
        return rect
    }

    // MARK: - Positioning Overrides
    /**
    Calculate the bounds for the title label. Override to create a custom size title field.
    - parameter bounds: The current bounds of the title
    - parameter editing: True if the control is selected or highlighted
    - returns: The rectangle that the title label should render in
    */
    open func titleLabelRectForBounds(_ bounds: CGRect, editing: Bool) -> CGRect {
        if editing {
            return CGRect(x: 0, y: 10, width: bounds.size.width, height: titleHeight())
        }
        return CGRect(x: 0, y: titleHeight(), width: bounds.size.width, height: titleHeight())
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM