3

In Objective-C I was able to make a call like so, i.e. pass "no" options:

[string boundingRectWithSize:size options:0 attributes:attributes context:nil]

How can I tell a corresponding call in Swift to do the same. For instance, passing NSStringDrawingOptions(rawValue: 0)! produces a runtime error.

Drux
  • 11,992
  • 13
  • 66
  • 116
  • Sorry, no I didn't. I thought it is converted as a struct but it is actually an enum. The only chance I see is to use `TruncatesLastVisibleLine` because the comments say `Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.`. – HAS Dec 10 '14 at 14:05
  • @HAS Yep, that's also my current work-around. – Drux Dec 10 '14 at 14:07
  • If you look in the Objc header of `NSStringDrawingOptions` they are declared as with values of `1 << 5, 1 << 0, 1 << 1, 1 << 3`. I don't know to which value they default to in Objc but in Swift is not possible to use another value. – HAS Dec 10 '14 at 14:10
  • 1
    @HAS Your last comment pretty much nails it down. If you want to write it up as an answer, I'll accept it and stay with the work-around. – Drux Dec 10 '14 at 14:20
  • 1
    This is a long lived bug in Swift iOS SDK. see related: http://stackoverflow.com/q/24064650/3804019 – rintaro Dec 10 '14 at 14:23

2 Answers2

3

If you take a look at the Objective-C header you'll notice that NSStringDrawingOptions is declared as an NS_ENUM which is why it gets converted as an enum in Swift (opposed to NS_OPTIONS which gets converted to struct).

typedef NS_ENUM(NSInteger, NSStringDrawingOptions) {
    NSStringDrawingTruncatesLastVisibleLine = 1 << 5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.
    NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // The specified origin is the line fragment origin, not the base line origin
    NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights
    NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds
} NS_ENUM_AVAILABLE_IOS(6_0);

Due to that you cannot set it in Swift to any another value but the available enum cases (without getting a runtime error).

As @rintaro pointed out in the comments above this is considered a bug in iOS (it is resolved in OS X 10.10)

HAS
  • 19,140
  • 6
  • 31
  • 53
1

I have an answer from Apple Developer Relations on this (I raised a related bug).

The answer is that you pass the empty option set [] when you want no options.

function definition (swift3):

boundingRect(with size: NSSize, options: NSStringDrawingOptions = [], attributes: [String : AnyObject]? = [:]) -> NSRect

so

str.boundingRect(with:NSSize(width: 100, height: 100), options:[], attributes: [:])

or as the empty values have defaults in the method definition you can skip them entirely - so just:

str.boundingRect(with:NSSize(width: 100, height: 100))
Confused Vorlon
  • 9,659
  • 3
  • 46
  • 49