我正在创建一个应用程序,我想根据文本设置自定义UIView的框架,然后我在UIView上添加UILabel。但有时我使用下面的代码在运行时没有得到正确的框架:
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];
我想要输出如下截图:
你可以帮助我,我的代码在哪里做错了吗?谢谢