Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

swift - How to pass multiple enum values as a function parameter

How would I do the following - passing two NSStringDrawing options as a function parameter in swift:

CGRect boundingRect = [string boundingRectWithSize:CGSizeMake(280.0, NSIntegerMax)
                                                      options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                   attributes:options context:nil];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Edit: In Swift 3.0:

let options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]

Edit: This is how you would use the options enum in Swift 2.0:

let options: NSStringDrawingOptions = [.UsesLineFragmentOrigin, .UsesFontLeading]

Edit: The issue has been resolved in iOS 8.3 SDK Beta 1 (12F5027d):

Modified NSStringDrawingOptions [struct]

  • From: enum NSStringDrawingOptions : Int
  • To: struct NSStringDrawingOptions : RawOptionSetType

You can now write:

let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading

After some research and and @Anton Tcholakov's "comment":

  1. If you're targeting OS X 10.10, this is as simple way to do it:

    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)
    
  2. However, in iOS 8 SDK (in the current seed), there's a bug, where NSStringDrawingOptions is ported to Swift as enum : Int, instead of struct : RawOptionSet. You should send a bug report to Apple describing this serious problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...