說我有3個視圖,每個視圖中都有一個標籤。現在我想要的是,基於標籤內容,這些標籤的內容會根據不同的文本大小和長度爲按鈕的不同實例單擊我希望我的視圖適合標籤大小。如何實現動態視圖
如果視圖是紅色,紅色應括基於標籤的高度和寬度,其改變每次標籤的文本..
類VerticalFitLayout:VerticalLayout的{
override init(width: CGFloat) {
super.init(width: width)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
var height: CGFloat = 0
var zeroHeightView: UIView?
for i in 0..<subviews.count {
var view = subviews[i] as UIView
height += yOffsets[i]
if view.frame.height == 0 {
zeroHeightView = view
} else {
height += view.frame.height
}
}
if height < superview!.frame.height && zeroHeightView != nil {
zeroHeightView!.frame.size.height = superview!.frame.height - height
}
super.layoutSubviews()
}
}
我是否正在朝着正確的方向前進?
看到這個圖片http://imageresize.org/Output/6e2d4598-355c-4c0c-ae9e-9ce0a4fad574.png 但我想要的是一個視圖將根據文本而改變。標籤接收來自一個陣列或網絡服務的任何輸入..
你可以使用自動佈局。 https://stackoverflow.com/questions/42340026/make-label-only-as-wide-as-it-needs-to-be/42340556#42340556 –
http://imageresize.org/Output/6e2d4598-355c- 4c0c-ae9e-9ce0a4fad574.png。這是我想要的..我想調整基於文本長度的視圖,我只有一個視圖將接收可變長度的文本 –
是的這是可能的自動佈局。 –