The people who have used the options are all aware that a few vertical bars have put these values together. Like what:
+ (NSStringDrawingOptions)combine{
return NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading;
}
It's a very simple thing. Why does this have to come up with an article to say specifically? Because, Apple has bug! Yes, you do this in swift, and you know what I mean by Swift's grammar. Like what:
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)
The one is not work. The compiler will error:could not find memeber ' Useslinefragmentorigin '. But the code still has to be written like this. It can only be written when meeting the need to meet these different options.
But what if there's a bug like that? Okay, you're driving xcode, you can't find a ready-made bug? Think about it, what would you think? It has to be OC. And very convenient, although still detour, hey.
In a swift project, when adding or importing OC files, Xcode prompts you to create a bridge header (Bridge Street file, we'll simply call it a bridgehead). If the system does not give you, you can also handle, and then in the build settings to specify the path of your own bridgehead. This specific will be described in this series.
After creating the OC file, it is good to implement the functions you need, such as:
@implementation Utils
+ (NSStringDrawingOptions)combine{
return NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading;
}
@end
I don't have to write about the header files. is to declare that the class name is Utils, and this class contains a class method called combine.
As long as the header file of this OC file in the bridgehead file is import , it can be used directly in the swift file. You don't have to have import ideas and actions in any swift code. This is the case when you use it:
let options = Utils.combine()
let calculateSize = CGSize(width: width, height: 0 as Double)
var rect = content.boundingRectWithSize(calculateSize, options: options, attributes: [NSFontAttributeName: font], context: nil)
Very simple, this problem is solved. After that Apple changed his bug.
From objective-c to Swift, you have to be. (ii) Portfolio options