I'm trying to make a dynamic cell with a label using swift to support IOS7 in heightForRowAtIndexPath , but I only find objective-c code, is it possible for someone else to help me rewrite this code to fast?
// Fetch yourText for this row from your data source.. NSString *yourText = [yourArray objectAtIndex:indexPath.row]; CGSize lableWidth = CGSizeMake(300, CGFLOAT_MAX); // 300 is fixed width of label. You can change this value CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:19] constrainedToSize:lableWidth lineBreakMode:NSLineBreakByWordWrapping]; // You can put your desire font // Here, you will have to use this requiredSize and based on that, adjust height of your cell. I have added 10 on total required height of label and it will have 5 pixels of padding on top and bottom. You can change this too. int calculatedHeight = requiredSize.height+10; return (float)calculatedHeight;
exactly this statement (how to convert it to quick):
CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:19] constrainedToSize:lableWidth lineBreakMode:NSLineBreakByWordWrapping]
I tried converting it (but it does not work like the original objective-c):
var requiredSize: CGSize = originalString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(19.0)])
Aaoii source share