uitextfield左视图中的标签未与文本对齐

时间:2018-06-28 08:34:56

标签: ios uitextfield uilabel placeholder

我正在自定义textField,在他的左视图中有一个标签,但是标签的文本与textField的文本/占位符不对齐。

这是我的简单代码:

 class mytextfield: UITextField {

    override init(frame: CGRect) {
        super.init(frame: frame)

        leftViewMode = .always
        let label = UILabel()

        label.text = "Hello"
        leftView = label
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: 0, y: 0, width: 40, height: bounds.height)
    }
}

在模拟器上显示结果(我用红色下划线说明了区别)并调试视图层次结构:

enter image description here

enter image description here

该如何解决?

3 个答案:

答案 0 :(得分:1)

这可能是两个视图使用字体样式或框架的绑定大小进行渲染的问题。尝试为左侧标签视图调整y-positionheight

这不是您要查找的确切答案。但这可能会解决您的问题。

override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
    return CGRect(x: 0, y: 0, width: 40, height: (bounds.height - 4))
}

override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
    return CGRect(x: 0, y: -2, width: 40, height: bounds.height)
}

我已经尝试过了:

class LeftViewTextfield: UITextField {

    override init(frame: CGRect) {
        super.init(frame: frame)

        leftViewMode = .always
        let label = UILabel()
        label.backgroundColor = UIColor.yellow
        label.text = "Hello"
        label.font = UIFont.systemFont(ofSize: 16)
        leftView = label
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: 0, y: -0.5, width: 40, height: bounds.height)
    }
}


class TestViewController: UIViewController {

    var textField: LeftViewTextfield?
    override func viewDidLoad() {
        super.viewDidLoad()

        setupTextField()
    }

    func setupTextField() -> Void {
        textField = LeftViewTextfield(frame: CGRect(x: 40, y: 40, width: 100, height: 40))
        textField?.text = "Hello"
        textField?.font = UIFont.systemFont(ofSize: 16)
        textField?.backgroundColor = UIColor.orange
        self.view.addSubview(textField!)
    }

}

结果:

enter image description here

答案 1 :(得分:0)

您这样做:

action_dict = {'INT': 'intersection', 'ER': 'difference'}#associate the 'INT' with the intersection operation and so on
directory=input('Insert dir of the main folder')
with open(input()) as txtfile: #insert directory of txt
    x = txtfile.readlines()
for line in x:
    action, shape1, shape2 = line.split('_')
    print( action, shape1, shape2) # here line is ER_ASTOM_ASTIK or whatever line in the txt file
    if shape1 in sfiles and shape2 in sfiles:
        gpd.overlay(sfiles[shape1], sfiles[shape2], how=action_dict[action])

问题在于let label = UILabel() label.text = "Hello" leftView = label 的默认基线,插图布局,文本容器与UILabel的默认基线,插入布局和文本容器不同。

UITextFieldUITextView之间,它们也不相同。只需检查this related question(其他人也是如此)。作者叠加UILabelUITextView,而呈现方式则不同。

这样做的快速方法是将UILabel设置为UITextField,以避免重复UILabel的整个内部布局设置并将其复制到leftView上。只需将其设置为“不可选择/不可编辑”,它便会产生完美的幻觉。

答案 2 :(得分:0)

func setLeftIconwithLabel(_ icon: UIImage) {
    
    let padding = 10
    let screenWidth = UIScreen.main.bounds.width
    let size = Int(screenWidth*0.04)
    
    let outerView = UIView(frame: CGRect(x: 0, y: 0, width: 70, height: size) )
    outerView.backgroundColor = .clear
    let iconView  = UIImageView(frame: CGRect(x: padding, y: 0, width: size, height: size))
    iconView.image = icon
    let label = UILabel(frame: CGRect(x: iconView.frame.origin.x+iconView.frame.width + 5, y: 0, width: 40, height: 15))
    label.text = "+ 965 "
    label.font = UIFont(name: fontName.ProximaNova_Regular, size: Font.regular12)
    outerView.addSubview(iconView)
    outerView.addSubview(label)
    
    leftView = outerView
    leftViewMode = .always
    
}

txtEmail.setLeftIconwithLabel(UIImage(named: "mobile") ?? UIImage())
txtEmail.placeholder = "Mobile Number"