ios – NSStringDrawingOptions gives Expected a type error when using Objective-C classes in Swift

Question: Question:

I would like to dynamically change the line height with SWift according to the number of characters.
The target will be compatible with both iOS 7 and iOS 8.

So I wrote the following code, but I got an error and couldn't use it.
There is no problem if you specify either option, but this time it seems that you need both.

let options: NSStringDrawingOptions = (NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading)

When I investigated this error, I found information that it was a bug in iOS 7.
So when I tried to make only this part of the code an Objective-C class,
NSStringDrawingOptions gives the error Expected as type. Is there a way to get it through?

Bridging-Header is created, and other classes (SDWebImage etc.) are working safely, so I think that there is no problem.

Thank you.

The code is below.


#import <Foundation/Foundation.h>
@interface StringDrawingOptions : NSObject
+ (NSStringDrawingOptions)combine:(NSStringDrawingOptions)option1 with:(NSStringDrawingOptions)option2;


#import "StringDrawingOptions.h"
@implementation StringDrawingOptions
+ (NSStringDrawingOptions)combine:(NSStringDrawingOptions)option1 with:(NSStringDrawingOptions)option2
    return (option1 | option2);

Answer: Answer:

I think the error message Expected a type appears in DrawingOptions.h, so add #import <UIKit/UIKit.h> to the import statement in DrawingOptions.h.

NSStringDrawingOptions is an extension of UIKit, so you can't see the definition of NSStringDrawingOptions without importing UIKit.

Next, StringDrawingOptions is needed because it's a Swift bug (strictly speaking, the definition of NSStringDrawingOptions is wrong).

let options:NSStringDrawingOptions = (NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading)

The part of

let options = StringDrawingOptions.combine(.UsesLineFragmentOrigin, with: .UsesFontLeading)

And, let's calculate the bitmask using the Objective-C method defined in StringDrawingOptions.

Scroll to Top