在占位符字段中创建空格

creating spaces in placeholder fields

我的问题是我想为一个文本字段放置 2 个占位符。其中一个占位符应该在左边,另一个应该在右边。

我的代码:

func returnPlaceHolder(first: String, second: String, textField: UITextField){

    let width: Int = Int(textField.bounds.width * 0.2)
    let spaceValue = width - (first.count + second.count)
    var temp = "\(first) "
    let tempCount = spaceValue - (first.count + second.count)
    var value = String()

    for _ in 0..<tempCount {
       temp.append(" ")


    }


    value = "\(temp) \(second)"
    textField.placeholder = value
    textField.setLeftPaddingPoints(10)
    textField.setRightPaddingPoints(10)


}

我目前正在使用此功能创建空格..但我的问题是多个文本字段的空格不会相同,我希望它们对齐..

就像这张图:https://imgur.com/pZZMoNv

这是我当前代码得到的结果:https://imgur.com/a/5AN8EXl

不要介意 textFields 格式和文本,我稍后可以修复它们。我只想能够对齐 textFields 占位符。

很难(如果可能的话)通过在文本中插入空格来实现您正在尝试做的事情,因为除非您使用等宽字体,否则字体中的每个字符都有不同的宽度。

https://en.wikipedia.org/wiki/Monospaced_font

相反,我会推荐一种不同的方法。覆盖文本字段,提供两个 UILabel 并使用自动布局调整它们的位置。