0

我使用了 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

请选择您的行业和技能

4

1 回答 1

1

您必须在 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())
    }

于 2021-06-14T07:34:38.267 回答