Edwjonn Edwjonn - 7 months ago 25
Swift Question

Cannot convert value of type "CGRect" to specified type "CGSize"

I'm trying to write some 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'd like 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 advance!!!

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