我如何执行以下操作 - 在swift中将两个NSStringDrawing选项作为函数参数传递:
CGRect boundingRect = [string boundingRectWithSize:CGSizeMake(280.0, NSIntegerMax)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:options context:nil];
答案 0 :(得分:68)
编辑:在 Swift 3.0 :
let options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
编辑:以下是使用Swift 2.0中的选项枚举的方法:
let options: NSStringDrawingOptions = [.UsesLineFragmentOrigin, .UsesFontLeading]
修改:问题has been resolved in iOS 8.3 SDK Beta 1 (12F5027d) :
已修改
NSStringDrawingOptions
[struct]
- 来自:
enum NSStringDrawingOptions : Int
- 收件人:
struct NSStringDrawingOptions : RawOptionSetType
您现在可以写:
let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading
经过一些研究和@Anton Tcholakov的“评论”:
如果您的目标是OS X 10.10,这是一种简单的方法:
let size = CGSize(width: 280, height: Int.max)
let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading
let boundingRect = string.bridgeToObjectiveC().boundingRectWithSize(size, options: options, attributes: attributes, context: nil)
但是,在iOS 8 SDK中(在当前种子中),存在一个错误,其中NSStringDrawingOptions
被移植到Swift enum : Int
,而不是struct : RawOptionSet
。您应该向Apple发送一个错误报告来描述这个严重的问题。
答案 1 :(得分:21)
更新了Xcode 6.3的答案:
在Xcode 6.3 Beta(Swift 1.2)中,这最终得到修复,你现在可以这样做:
let boundingRect = "string".boundingRectWithSize(size, options: .UsesLineFragmentOrigin | .UsesFontLeading, attributes:nil, context:nil)
旧版本:
它看起来像当前测试版中的一个错误,现在我编写Objective-C方法并使用它来自Swift:
+ (NSStringDrawingOptions)combine:(NSStringDrawingOptions)option1 with:(NSStringDrawingOptions)option2
{
return option1 | option2;
}
并致电Swift表格:
let boundingRect = "string".boundingRectWithSize(size, options: StringDrawingOptions.combine(.UsesLineFragmentOrigin, with: .UsesFontLeading), attributes:nil, context:nil)
答案 2 :(得分:7)
另一种解决方案,使用unsafeBitCast
。
如下所示:
let options = unsafeBitCast(NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue |
NSStringDrawingOptions.UsesFontLeading.rawValue,
NSStringDrawingOptions.self)
答案 3 :(得分:3)
就像是
theStringDrawingOptions = [NSStringDrawingOptions.UsesLineFragmentOrigin,
NSStringDrawingOptions.UsesFontLeading]
参见讲座wwdc 2015 - > 106_hd_whats_new_in_swift在6分钟内
答案 4 :(得分:3)
在Swift 2.1中我们可以使用,
calculatedRect = (text as NSString).boundingRectWithSize(bounds.size, options: NSStringDrawingOptions([.UsesLineFragmentOrigin, .UsesFontLeading]), attributes: [NSFontAttributeName : font], context: nil)
为参数传递多个枚举值。
答案 5 :(得分:1)
似乎无法评论akashivskyy的帖子(没有50多个声望),但这似乎在我的结尾:
import Cocoa
let drawingOpts : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading
let boundingRect = "test".bridgeToObjectiveC().boundingRectWithSize(CGSize(width: 280, height: Int.max), options: drawingOpts, attributes: nil)
此外,如果我Cmd +单击NSStringDrawingOptions,我将其视为继承自RawOptionSet。
答案 6 :(得分:0)
Swift支持C的按位OR运算符,但枚举值现在在特定范围内,而不仅仅是全局符号:
let boundingRect = "fpp".boundingRectWithSize(CGSizeMake(280.0, CGFloat.max),
options:NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
attributes:nil, context:nil)
答案 7 :(得分:0)
在 swift 4.0 + 中,我们可以传递多个类似的选项,对我有用
let options = NSStringDrawingOptions(rawValue: NSStringDrawingOptions.usesLineFragmentOrigin.rawValue | NSStringDrawingOptions.usesFontLeading.rawValue)
let boundingRect = string._bridgeToObjectiveC().boundingRect(with: CGSize(width: 280, height: Int.max), options: options, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 19.0)], context: nil)