I have a piece of code that computes for the height a block of text is going to use up and set the uiLabel accordingly:
text = [self tableText:[indexPath row] forIndex:index];
constraint = CGSizeMake(detailWidth, 20000.0f);
size = [text sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
UILabel *presidentDetailLabel = (UILabel *)[cell viewWithTag:200];
[presidentDetailLabel setFrame:CGRectMake(presidentDetailLabel.frame.origin.x, presidentDetailLabel.frame.origin.y, presidentDetailLabel.frame.size.width, MAX(size.height, presidentDetailLabel.frame.size.height))];
[presidentDetailLabel setText:text];
The code works fine on iOS 6 but when I compiled it on iOS 7 and ran it on an emulator. The size returned seems to be a few pixels short. I actually have a NSlog to check if the fontsize and the value returned is 16.
Same problem with a similar block of code only this time I'm using attributed string and it still returns a height a few pixels short. From what I gather. It seems that the text is using up the space equivalent of font size 17.
Any advice how to hunt this bug down?
I am using following lines in some of my projects and always work like charm in iOS 5 and above (didn't use in lower versions).
CGSize constraint = CGSizeMake(label.frame.size.width, 2000.0f);
CGSize size;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [label.text boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:label.font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
[label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, size.height)];
I fixed my problem using this code:
NSString *text = [self tableText:[indexPath row] forIndex:index];
UILabel *presidentDetailLabel = (UILabel *)[cell viewWithTag:200];
[presidentDetailLabel setText:text];
CGSize size = [presidentDetailLabel sizeThatFits:CGSizeMake(presidentDetailLabel.frame.origin.y, 2000)];
[presidentDetailLabel setFrame:CGRectMake(presidentDetailLabel.frame.origin.x, presidentDetailLabel.frame.origin.y, presidentDetailLabel.frame.size.width, size.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.