From objective-c to Swift, you have to be. (ii) Portfolio options

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.