gpt4 book ai didi

ios - 计算 UITextField 基线位置

转载 作者:技术小花猫 更新时间:2023-10-29 10:54:15 25 4
gpt4 key购买 nike

我正在尝试以编程方式将 UITextField 实例的基线与 UIButton 实例对齐。我目前正在以类似的方式处理此计算 here , 但对于 UITextField ,内容可以有一个偏移量。有什么想法可以让我在 UITextField 中获得文本位置,甚至更好,是否有更简单的方法来对齐基线?

最佳答案

根据我的经验,UITextField 实际上在文本周围没有任何偏移,这与 UIButton 不同。无论如何,您可以找出两个控件中的确切文本框:

let textField = UITextField()
//... size the field, e.g. textField.sizeToFit()

let button = UIButton()
//... size the button, e.g. button.sizeToFit()

// Compute the target offset between tops (frame.origin.y).
let buttonBox = button.contentRectForBounds(button.bounds)
let buttonBaseline = buttonBox.origin.y + button.titleLabel!.font.ascender

let textFieldBox = textField.textRectForBounds(textField.bounds)
let textFieldBaseline = textFieldBox.origin.y + textField.font!.ascender

let offset = buttonBaseline - textFieldBaseline

//... position button, e.g., button.frame = CGRect(...)

textField.frame = CGRect(
origin: CGPoint(x: button.frame.maxX, y: button.frame.origin.y + offset),
size: textField.bounds.size)

Example alignment with different font sizes

关于ios - 计算 UITextField 基线位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com