Ios 带sum的UILabel问题
我的UILabel有一个长文本(文本因不同的情况和语言而不同,我使用AttributeString),它以amount结尾。Smth类似:Ios 带sum的UILabel问题,ios,swift,nsstring,uilabel,nsattributedstring,Ios,Swift,Nsstring,Uilabel,Nsattributedstring,我的UILabel有一个长文本(文本因不同的情况和语言而不同,我使用AttributeString),它以amount结尾。Smth类似: This amount will be taken from your account: 12.00 $ 标签的numberOfLines=0,所以有时文本看起来像 This amount will be taken from your account: 12.00 $ 不要将换行符设置在12.00和$之间,这一点非常重要,因为它看起来非常糟糕。有没有
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
$
有什么特殊的符号像html,所以我可以把它
在12.00和美元之间
对于这种情况,可以使用nobreak空格字符\u00a0
示例:
let text = "This amount will be taken from your account: 12.00\u{00a0}$"
有什么特殊的符号像html,所以我可以把它
在12.00和美元之间
对于这种情况,可以使用nobreak空格字符\u00a0
示例:
let text = "This amount will be taken from your account: 12.00\u{00a0}$"
因此,请确保没有在标签上设置高度约束,然后执行此操作
var value = 12.00
yourLabel.numberOfLines = 0
yourLabel.text = "This amount will be taken from your account: \n \(value) $"
因此,请确保没有在标签上设置高度约束,然后执行此操作
var value = 12.00
yourLabel.numberOfLines = 0
yourLabel.text = "This amount will be taken from your account: \n \(value) $"
您可以添加不间断的unicode空格字符,但是,您所做的是错误的 您应该使用
NSNumberFormatter
,样式设置为.currency
,而currencyCode
设置为USD
这将给你:
$12.00
,英语为负金额为$(12.00)
,欧洲地区为12.00 US$
)您可以添加不间断的unicode空格字符,但是,您所做的是错误的 您应该使用
NSNumberFormatter
,样式设置为.currency
,而currencyCode
设置为USD
这将给你:
$12.00
,英语为负金额为$(12.00)
,欧洲地区为12.00 US$
)let formatter = NumberFormatter()
formatter.numberStyle = .currency
if let amount = formatter.string(from: 12.0 as NSNumber) {
print(amount) // $12.00
}
请注意,我标记为重复的问题
如中所述,您需要使用
我想建议您可能希望用于处理货币字符的内容,类似于:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
if let amount = formatter.string(from: 12.0 as NSNumber) {
print(amount) // $12.00
}
看到这一次,我不认为这是一个重复,因为在这种情况下,用非破坏性空间替换空间的通用解决方案不是最优的。看到这一次,我不认为这是一个重复,因为用非破坏性空间替换空间的通用解决方案在这种情况下不是最优的。我不确定谁投了反对票,谁投了反对票答案和原因?至少留下一个理由。您好,我认为您的答案被否决了,因为它没有给出OP的预期结果。这将始终让值和“$”位于一个新行上,这里不是这样。更详细地说,如果“这个金额将从您的帐户中扣除:”只是“扣除的金额”呢?我不知道是谁投了反对票,并回答了为什么?至少留下一个理由。您好,我认为您的答案被否决了,因为它没有给出OP的预期结果。这将始终让值和“$”位于一个新行上,这里不是这样。更详细地说,如果“此金额将从您的帐户中提取:”只是“提取金额”?则无需将格式化程序区域设置为当前区域设置。这是默认值。此外,locale是一个结构。您只需
formatter.locale=locale.current
@Sulthan感谢您的注释。无需将格式化程序区域设置设置为当前区域设置。这是默认值。此外,locale是一个结构。您只需formatter.locale=locale.current
@Sulthan感谢您的留言。