更改约束时adjustsFontSizeToFitWidth不起作用?

时间:2016-01-16 06:33:55

标签: ios swift uilabel nslayoutconstraint

我有一个costView,其中包含UILabel dollarSignUILabel costThis is how dollarSign is being constrained. This is how cost is being constrained.

AddViewController我更改了一些约束,包括costView的高度

class AddViewController: UIViewController {


@IBOutlet weak var costView: UIView!
@IBOutlet weak var statusBarHeight: NSLayoutConstraint!
@IBOutlet weak var navigationBar: UINavigationBar!
@IBOutlet weak var dollarSign: UILabel!
@IBOutlet weak var cost: UILabel!
@IBOutlet weak var keyboardHeight: NSLayoutConstraint!
@IBOutlet weak var costViewHeight: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()

    //Set size of cost view & numbers keyboard
    let halfViewHeight = (view.bounds.height - statusBarHeight.constant - navigationBar.bounds.height)/2
    costViewHeight.constant = halfViewHeight/CGFloat(4)
    keyboardHeight.constant = halfViewHeight - costViewHeight.constant

    //Adjust font size of dollar sign & cost
    cost.adjustsFontSizeToFitWidth = true
    print("cost frame: \(cost.frame)")
    dollarSign.adjustsFontSizeToFitWidth = true
    print("dollar sign frame: \(dollarSign.frame)")
}
}

当我加载应用时,约束costViewHeight& keyboardHeight相应地更新并重新排列。但是,标签dollarSigncost的字体大小不会自行调整,as you can see here.

在这里添加2行后:

    //Adjust font size of dollar sign & cost
    cost.adjustsFontSizeToFitWidth = true
    cost.backgroundColor = UIColor.redColor() //add this line
    print("cost frame: \(cost.frame)")
    dollarSign.adjustsFontSizeToFitWidth = true
    dollarSign.backgroundColor = UIColor.redColor() //add this line
    print("dollar sign frame: \(dollarSign.frame)")

You can see the frame of cost and dollarSign readjust themselves to fit in with the new constraints.

问题:由于我使用.adjustsFontSizeToFitWidth = true,为什么在更改高度后字体大小没有变化? .adjustFontSizeToFitWidth仅在更改宽度时有效吗?如果是这样,我该如何制作.adjustFontSizeToFitHeight方法?

我注意到一个奇怪的事情可能与否有关系是当我打印出costdollarSign帧时,它们与故事板上列出的帧完全相同(cost framedollarSign frame)。

cost frame: (567.0, 7.5, 14.5, 34.0)
dollar sign frame: (8.0, 8.0, 15.0, 34.0)

这是奇怪的b / c,因为我更改costView的高度costdollarSign的帧应更改(which they do)。但是,如果costdollarSign框架发生变化,那么为什么他们的数字不会改变?

0 个答案:

没有答案