Edwjonn Edwjonn - 7 months ago 15
Swift Question

Cannot conver value of type "CGRect" to speciefied type "CGSize"

I'm trying to write a code, but i get the error in the title... here is the code...

let text: NSString = messages[indexPath.row] as! NSString

var size:CGSize = text.boundingRectWithSize(CGSizeMake(240.0, 480.0), options: NSStringDrawingOptions.UsesDeviceMetrics, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14.0)], context: nil)


I get the error in the second line

Also, i have the objective-c code, which is ok... But i want it in swift

NSString *text = [messages objectAtIndex:indexPath.row];
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];


Thanks in advanced!!!

Answer

Try adding .size to the end like:

var size:CGSize = text.boundingRectWithSize(CGSizeMake(240.0, 480.0), options: NSStringDrawingOptions.UsesDeviceMetrics, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14.0)], context: nil).size
Comments