我有一个costView
,其中包含UILabel dollarSign
和UILabel cost
。 This 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
相应地更新并重新排列。但是,标签dollarSign
和cost
的字体大小不会自行调整,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
方法?
我注意到一个奇怪的事情可能与否有关系是当我打印出cost
和dollarSign
帧时,它们与故事板上列出的帧完全相同(cost
frame和dollarSign
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
的高度cost
和dollarSign
的帧应更改(which they do)。但是,如果cost
和dollarSign
框架发生变化,那么为什么他们的数字不会改变?