I am creating one application, where i want to set frame of custom UIView according to the text, then i am adding UILabel over the UIView. But sometimes i am not getting correct frame at run time using my below code :
strImageBottomText = @"The next-generation entertainment for first web view just bottom of slide show image.";
CGSize maximumSize = CGSizeMake(320, 9999);
CGFloat cellHeight = 0;
CGSize titleLabelHeightSize = CGSizeZero;
if ([strImageBottomText respondsToSelector: @selector(boundingRectWithSize:options:attributes:context:)] == YES) {
titleLabelHeightSize = [strImageBottomText boundingRectWithSize: maximumSize options: NSStringDrawingUsesLineFragmentOrigin
attributes: @{ NSFontAttributeName: [UIFont fontWithName:@"Cabin-SemiBold" size:15.0f] } context: nil].size;
} else {
titleLabelHeightSize = [strImageBottomText sizeWithFont: [UIFont fontWithName:@"Cabin-SemiBold" size:15.0f]
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
}
cellHeight += titleLabelHeightSize.height > 24 ? titleLabelHeightSize.height : 24;
CGRect frame = CGRectMake(0, slideshow.frame.size.height, 320, cellHeight + 30);
UIView *customView = [[UIView alloc] initWithFrame:frame];
customView.backgroundColor = [UIColor colorWithRed:0/255.0 green:151/255.0 blue:207/255.0 alpha:1.0];
UILabel *sectionTitle = [[UILabel alloc] init];
sectionTitle.frame = CGRectMake(0, 0, 280, cellHeight);
sectionTitle.text = strImageBottomText;
sectionTitle.lineBreakMode = NSLineBreakByWordWrapping;
sectionTitle.textAlignment = NSTextAlignmentCenter;
sectionTitle.numberOfLines = 0;
[sectionTitle sizeToFit];
sectionTitle.font = [UIFont fontWithName:@"Cabin-SemiBold" size:15.0f];
sectionTitle.backgroundColor = [UIColor clearColor];
sectionTitle.textColor = [UIColor whiteColor];
[customView addSubview:sectionTitle];
[self.view addSubview:customView];
I want my output as below screenshot :
Can you please help me that where i am doing wrong in my code? Thanks