如何垂直和水平对齐 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 位置作为波纹管覆盖功能
// 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())
    }