【问题标题】:creating spaces in placeholder fields在占位符字段中创建空间
【发布时间】:2019-12-29 18:48:34
【问题描述】:

我的问题是我想为一个文本字段放置 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 占位符。

【问题讨论】:

    标签: ios swift textfield placeholder


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 2023-03-07
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2018-09-29
      • 2019-08-17
      相关资源
      最近更新 更多