【问题标题】:UILabel issue with sum [duplicate]总和的UILabel问题[重复]
【发布时间】:2017-01-31 10:30:49
【问题描述】:

我的 UILabel 有一个很长的文本(不同情况和语言的文本不同,我使用 AttributedString),它以数量结尾。喜欢:

This amount will be taken from your account: 12.00 $

标签的 numberOfLines = 0,所以有时文本看起来像

This amount will be taken from your account: 12.00 
$

不要在 12.00 和 $ 之间设置换行符非常重要,因为它看起来很糟糕。有没有什么特殊符号像   在 html 中,所以我可以把它放在 12.00 和 $ 之间?所以 UILabel 从来没有在 12 和 $ 之间设置过中断?

所以我想要的是:

This amount will be taken from your account: 
12.00 $

This amount will be taken from your account: 12.00 $

This amount will be taken from 
your account: 12.00 $

但它不应该是这样的

This amount will be taken from your account: 12.00 
$

【问题讨论】:

标签: ios swift nsstring uilabel nsattributedstring


【解决方案1】:

html中是否有特殊符号如,我可以把它 在 12.00 和 $ 之间?

对于这种情况,您可以使用无间断空格字符\u00a0

示例:

let text = "This amount will be taken from your account: 12.00\u{00a0}$"

【讨论】:

    【解决方案2】:

    您可以添加不间断的 unicode 空格字符,但是,您所做的是错误的。

    您应该使用NSNumberFormatter,并将样式设置为.currencycurrencyCode 设置为USD

    这会给你:

    1. 自动不破坏空间
    2. 所有语言的格式正确(即,$12.00 英语,$(12.00) 英语负数,12.00 US$ 欧洲语言环境)。

    【讨论】:

      【解决方案3】:

      请注意,我标记了一个重复的问题。

      正如duplicated question中提到的,你需要使用"No-Break Space" unicode character "\u00a0"

      我想建议您可能想使用NumberFormatter 来处理货币字符,类似于:

      let formatter = NumberFormatter()
      formatter.numberStyle = .currency
      if let amount = formatter.string(from: 12.0 as NSNumber) {
          print(amount) // $12.00
      }
      

      【讨论】:

      • 无需将格式化程序语言环境设置为当前语言环境。这是默认设置。此外,语言环境是一个结构。你可以formatter.locale = Locale.current
      • @Sulthan 谢谢你的来信。
      【解决方案4】:

      因此,请确保您没有在标签上设置高度限制,然后执行此操作。

      var value = 12.00
      yourLabel.numberOfLines = 0
      yourLabel.text = "This amount will be taken from your account: \n \(value) $"
      

      【讨论】:

      • 我不确定谁投了反对票并回答了,为什么?至少留下一个理由。
      • 您好,我认为您的答案被否决了,因为它没有给出 OP 的预期结果。这将总是让值和“$”在一个新行上,这里不是这种情况。详细来说,如果“This amount will be made from your account:”只是“take amount”怎么办?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多