无法将“CGRect”类型的值转换为指定类型“CGSize”

时间:2016-04-20 19:57:27

标签: objective-c swift

我正在尝试编写一些代码,但我在标题中得到了错误......这是代码...

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)

我在第二行得到错误

另外,我有Objective-C代码,没问题......但我喜欢它在Swift中

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

提前致谢!!!

1 个答案:

答案 0 :(得分:6)

尝试将.size添加到最后,例如:

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