I have a (vertical) UIStackView
containing both a plain UIView
of height 50
(named sliderView
) and a UILabel
of height 36
defined in my storyboard. The label's alpha
property is initially set to 0.0
to make it invisible.
In the controller's viewDidLoad
I use UIViewController Containment to add another view controller's view to as a subview of sliderView
. This new subview does not necessarily match sliderViews
height. It might actually a fair bit taller.
At first, this setup looks fine. Once I make the label visible, I see that it still starts at a y-position of 50
. So, the sliderView
did not automatically stretch to use it's new child's height. Makes sense.
I thought that I could easily just call sizeToFit
on sliderView
to make those two heights fit. Unfortunately, this does not seem to work. Am I misunderstanding something here? Thanks!
Use following method to get the CGSize
required for NSString
text.
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height+20.0f);
}
return size;
}
Here width
parameter is the width of your label
and font
is the font specified for your label
. Call CGSize.height
to get the height.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.