【问题标题】:How to automatically adjust the width of a centered UITextfield in Swift while editing?编辑时如何在 Swift 中自动调整居中 UITextfield 的宽度?
【发布时间】:2015-12-28 11:07:47
【问题描述】:

我有一个自定义 UITextfield,它通过约束以 x 为中心位于其超级视图。它还在文本下方有一个高度约束和一条漂亮的细线,与文本字段本身具有相同的宽度(就像我说的:自定义)。 我现在想在编辑其内容时保持 UITextfield 居中,并根据其字符串内容调整自身的宽度。

现在发生的是它在编辑时保持其宽度,裁剪其重叠内容。

我的代码有点

// on textfields editing change event
@IBAction func nameFieldEditingChanged(sender: AnyObject) {
    // set width of textfield to width of textstring
    checkUserNameTextfieldWidth()
}

func checkUserNameTextfieldWidth(){
    println("checkUserNameTextfieldWidth \(userName.text) / \(userName.frame.size.width) / \(view.bounds.width)")

    // something has to happen here I guess...

}

谢谢!

【问题讨论】:

    标签: swift xcode6 uitextfield


    【解决方案1】:

    您必须使用 UITextView 而不是 UITextField 并应用其 sizeThatFits 方法(请参阅this response

    【讨论】:

    • 当我这样做时,我会松开“x-Delete-TextField-Content”选项,对吧?
    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2017-06-24
    相关资源
    最近更新 更多