Ios 调整标签大小以适应值

Ios 调整标签大小以适应值,ios,swift,swift3,xcode8,Ios,Swift,Swift3,Xcode8,我试图调整标签的大小以适应文本量,但发布的答案需要更多的解释 对我来说,我有三个标签:货币、整数金额、双倍金额: 我有三个标签,因为货币和双倍金额的样式与整数金额的样式不同。如果我走错了方向,请纠正我,因为我不可能三个都只有一个标签 这三个都有一个自动调整大小的右上方 最终,我必须删除静态值,但当我应用下面的代码时,注意: viewDidLoad()或ViewDidDisplay(): integerAmountLabel.sizeToFit() integerAmountLabel.tex

我试图调整标签的大小以适应文本量,但发布的答案需要更多的解释

对我来说,我有三个标签:货币、整数金额、双倍金额:

我有三个标签,因为货币和双倍金额的样式与整数金额的样式不同。如果我走错了方向,请纠正我,因为我不可能三个都只有一个标签

这三个都有一个自动调整大小的
右上方

最终,我必须删除静态值,但当我应用下面的代码时,注意:

viewDidLoad()或ViewDidDisplay():

integerAmountLabel.sizeToFit()

integerAmountLabel.text = "1"
// or integerAmountLabel = "280,000"

期望值:
1.00英镑
280000.00英镑
。我得到的:
.1.00
.1.00

您可以使用单个标签,这将解决所有问题,而且您不必与标签样式妥协。这可以通过使用NSAttributedString实现。下面显示了一个示例,您可以参考该示例的输出

    let string = NSMutableAttributedString(string: "1000.12")
    string.addAttribute(NSFontAttributeName,value: UIFont.systemFont(ofSize: 25.0), range: NSRange(location: 0, length: 4))
    string.addAttribute(NSFontAttributeName,value: UIFont.systemFont(ofSize: 10.0), range: NSRange(location: 5, length: 2))

您可以使用单个标签,这将解决所有问题,而且您不必妥协于标签样式。这可以通过使用NSAttributedString实现。下面显示了一个示例,您可以参考该示例的输出

    let string = NSMutableAttributedString(string: "1000.12")
    string.addAttribute(NSFontAttributeName,value: UIFont.systemFont(ofSize: 25.0), range: NSRange(location: 0, length: 4))
    string.addAttribute(NSFontAttributeName,value: UIFont.systemFont(ofSize: 10.0), range: NSRange(location: 5, length: 2))

正如Aman Gupta已经提到的,使用属性字符串。下面是一个操场片段,解释如何做到这一点:

import UIKit
import PlaygroundSupport

var str = "Hello, playground"

let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 300))
PlaygroundPage.current.liveView = view

// set up view hierarchy
view.backgroundColor = .blue
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[label]-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["label": label]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[label]-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["label": label]))

// set up atributes
let currencyAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20), NSForegroundColorAttributeName:  UIColor.white]
let integerAmountAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 30), NSForegroundColorAttributeName: UIColor.white]
let decimalAmountAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16), NSForegroundColorAttributeName: UIColor(white: 1, alpha: 0.7)]


// set up formatter
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.currency
formatter.locale = Locale(identifier: "en_GB")

let amount = 8001.9
let text = formatter.string(from: NSNumber(value: amount))!
let nsText = text as NSString

// calculate ranges
let currencyRange = NSRange(location: 0, length: 1)
let decimalPointRange = nsText.range(of: ".")
var integerAmountLocation = currencyRange.location + currencyRange.length
var integerAmountLength = decimalPointRange.location - integerAmountLocation
var integerAmountRange = NSRange(location: integerAmountLocation, length: integerAmountLength)

// configure attributed string
var attributedText = NSMutableAttributedString(string: text, attributes: decimalAmountAttributes)
attributedText.setAttributes(currencyAttributes, range: currencyRange)
attributedText.setAttributes(integerAmountAttributes, range: integerAmountRange)

label.attributedText = attributedText


你可以在这里看到整个游乐场:

正如Aman Gupta已经提到的,使用属性字符串。下面是一个操场片段,解释如何做到这一点:

import UIKit
import PlaygroundSupport

var str = "Hello, playground"

let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 300))
PlaygroundPage.current.liveView = view

// set up view hierarchy
view.backgroundColor = .blue
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[label]-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["label": label]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[label]-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["label": label]))

// set up atributes
let currencyAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20), NSForegroundColorAttributeName:  UIColor.white]
let integerAmountAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 30), NSForegroundColorAttributeName: UIColor.white]
let decimalAmountAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16), NSForegroundColorAttributeName: UIColor(white: 1, alpha: 0.7)]


// set up formatter
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.currency
formatter.locale = Locale(identifier: "en_GB")

let amount = 8001.9
let text = formatter.string(from: NSNumber(value: amount))!
let nsText = text as NSString

// calculate ranges
let currencyRange = NSRange(location: 0, length: 1)
let decimalPointRange = nsText.range(of: ".")
var integerAmountLocation = currencyRange.location + currencyRange.length
var integerAmountLength = decimalPointRange.location - integerAmountLocation
var integerAmountRange = NSRange(location: integerAmountLocation, length: integerAmountLength)

// configure attributed string
var attributedText = NSMutableAttributedString(string: text, attributes: decimalAmountAttributes)
attributedText.setAttributes(currencyAttributes, range: currencyRange)
attributedText.setAttributes(integerAmountAttributes, range: integerAmountRange)

label.attributedText = attributedText



你可以在这里看到整个游乐场:

你在使用
自动布局吗?@Rikh是的,我是。首先将文本设置为标签,然后调用标签上的sizeToFit方法。你能解释一下你在使用什么约束吗?应该解释为什么会这样。@Rikh对标签没有限制。让我试试devgr说的话。你在使用
autoLayout
?@Rikh是的,我是。首先将文本设置为标签,然后调用标签上的sizeToFit方法。你能解释一下你在使用什么约束吗?应该解释为什么会这样。@Rikh对标签没有限制。让我试试devgr所说的。我如何准确地将其设置为标签?只需使用UILabel的attributedText属性,您就可以将其设置为标签。例如label.attributedText=string我无法获得较小的
.12
,因为我已在标签上设置了字体大小请参见我们正在设置的属性适用范围内的文本,即NSRange(位置:0,长度:4)。您可以使用相同的方法添加多个属性,只需更改属性的范围。我也相应地更新了答案,请参考。按照您所说的做了,但我的
.12
仍然很大。你真的试过吗?我该如何将其设置为标签?只需使用UILabel的attributedText属性,你就可以将其设置为标签。例如label.attributedText=string我无法获得较小的
.12
,因为我已经在标签上设置了字体大小。请参见,我们正在设置的属性属性适用对于范围内的文本,即NSRange(位置:0,长度:4)。您可以使用相同的方法添加多个属性,只需更改属性的范围即可。我也相应地更新了答案,请参考。按照您所说的做了,但我的
.12
仍然很大。你真的试过这个吗?是的,先生。我接受这个。非常有用。我可能需要创建一些常量,因为我需要重用,例如,代码> CurrnyyAtts[/COD]别处。基金会有一个数字格式化程序类,它简化了格式化货币并使它与区域相关。代码>数字格式.样式.货币
我会更新答案是的,先生。我接受这个。非常有用。我可能需要创建一些常量,因为我需要重用,例如,代码> CurrnyyAtts[/COD]别处。基金会有一个数字格式化程序类,它简化了格式化货币并使它与区域相关。code>NumberFormatter.Style.currency我将更新答案