Option Sets intersections in Swift












0















Now I read Apple documentation about Core Text and I have one problem in understanding:



CTFontSymbolicTraits
conforms to OptionSet. And CTFontStylisticClass can be obtained via classMaskTrait option in CTFontStylisticClass.



Am I understand right that classMaskTrait option can includes all CTFontStylisticClass-options?. For example, if I want to detect sansSerifClass option in CTFontStylisticClass:



CTFontStylisticClass(rawValue: CTFontGetSymbolicTraits(font).rawValue).contains(.sansSerifClass)


is it right example checking?










share|improve this question

























  • Before we go answering this, may we think about whether Core Text is needed for what you are really trying to do? What are we really trying to do? This looks like something we could do much more easily with a font descriptor and never have to drop down to the level of Core Text. What is font and what question about it are you actually trying to answer?

    – matt
    Nov 26 '18 at 21:29











  • I want to detect stylistic qualities of the font

    – Ihor M.
    Nov 26 '18 at 21:32
















0















Now I read Apple documentation about Core Text and I have one problem in understanding:



CTFontSymbolicTraits
conforms to OptionSet. And CTFontStylisticClass can be obtained via classMaskTrait option in CTFontStylisticClass.



Am I understand right that classMaskTrait option can includes all CTFontStylisticClass-options?. For example, if I want to detect sansSerifClass option in CTFontStylisticClass:



CTFontStylisticClass(rawValue: CTFontGetSymbolicTraits(font).rawValue).contains(.sansSerifClass)


is it right example checking?










share|improve this question

























  • Before we go answering this, may we think about whether Core Text is needed for what you are really trying to do? What are we really trying to do? This looks like something we could do much more easily with a font descriptor and never have to drop down to the level of Core Text. What is font and what question about it are you actually trying to answer?

    – matt
    Nov 26 '18 at 21:29











  • I want to detect stylistic qualities of the font

    – Ihor M.
    Nov 26 '18 at 21:32














0












0








0


1






Now I read Apple documentation about Core Text and I have one problem in understanding:



CTFontSymbolicTraits
conforms to OptionSet. And CTFontStylisticClass can be obtained via classMaskTrait option in CTFontStylisticClass.



Am I understand right that classMaskTrait option can includes all CTFontStylisticClass-options?. For example, if I want to detect sansSerifClass option in CTFontStylisticClass:



CTFontStylisticClass(rawValue: CTFontGetSymbolicTraits(font).rawValue).contains(.sansSerifClass)


is it right example checking?










share|improve this question
















Now I read Apple documentation about Core Text and I have one problem in understanding:



CTFontSymbolicTraits
conforms to OptionSet. And CTFontStylisticClass can be obtained via classMaskTrait option in CTFontStylisticClass.



Am I understand right that classMaskTrait option can includes all CTFontStylisticClass-options?. For example, if I want to detect sansSerifClass option in CTFontStylisticClass:



CTFontStylisticClass(rawValue: CTFontGetSymbolicTraits(font).rawValue).contains(.sansSerifClass)


is it right example checking?







ios swift option optionsettype






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 21:51









Code Different

47.7k776111




47.7k776111










asked Nov 26 '18 at 21:16









Ihor M.Ihor M.

1155




1155













  • Before we go answering this, may we think about whether Core Text is needed for what you are really trying to do? What are we really trying to do? This looks like something we could do much more easily with a font descriptor and never have to drop down to the level of Core Text. What is font and what question about it are you actually trying to answer?

    – matt
    Nov 26 '18 at 21:29











  • I want to detect stylistic qualities of the font

    – Ihor M.
    Nov 26 '18 at 21:32



















  • Before we go answering this, may we think about whether Core Text is needed for what you are really trying to do? What are we really trying to do? This looks like something we could do much more easily with a font descriptor and never have to drop down to the level of Core Text. What is font and what question about it are you actually trying to answer?

    – matt
    Nov 26 '18 at 21:29











  • I want to detect stylistic qualities of the font

    – Ihor M.
    Nov 26 '18 at 21:32

















Before we go answering this, may we think about whether Core Text is needed for what you are really trying to do? What are we really trying to do? This looks like something we could do much more easily with a font descriptor and never have to drop down to the level of Core Text. What is font and what question about it are you actually trying to answer?

– matt
Nov 26 '18 at 21:29





Before we go answering this, may we think about whether Core Text is needed for what you are really trying to do? What are we really trying to do? This looks like something we could do much more easily with a font descriptor and never have to drop down to the level of Core Text. What is font and what question about it are you actually trying to answer?

– matt
Nov 26 '18 at 21:29













I want to detect stylistic qualities of the font

– Ihor M.
Nov 26 '18 at 21:32





I want to detect stylistic qualities of the font

– Ihor M.
Nov 26 '18 at 21:32












1 Answer
1






active

oldest

votes


















0














To understand these constants, let's look at the CTFontStylisticClass documentation:




The class values are bundled in the upper four bits of the CTFontSymbolicTraits and can be obtained via kCTFontClassMaskTrait.




To verify, let's look at the kCTFontClassMaskTrait documentation. If you set the language to Objective-C, the documentation shows the definitions of kCTFontClassMaskTrait:




kCTFontClassMaskTrait = kCTFontTraitClassMask



So it's just defined as another constant, which has all the same words in a different order. Ha ha, Apple, you're hilarious.



Okay, let's look at the kCTFontTraitClassMask documentation. Again, if you set the language to Objective-C, you can see the definition of the constant:




kCTFontTraitClassMask = (15U << kCTFontClassMaskShift)



Indeed, 15U is four consecutive 1 bits, and it's shifted left by some amount. This is typical of a “mask”: it defines a subset of the bits in a binary word.



To convert a CTFontSymbolicTraits to a CTFontStylisticClass, we need to use the mask to select just those bits from the CTFontSymbolicTraits raw value, and use the result as the raw value of a CTFontStylisticClass. We can do the selection by using the bitwise operator &, or by using the OptionSet method intersection.



What we really want, in Swift, is a method on CTFontSymbolicTraits that extracts a CTFontStylisticClass. So let's write an extension:



extension CTFontSymbolicTraits {
var stylisticClass: CTFontStylisticClass {
return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
}
}


Let's test it:



import CoreText
import Foundation

extension CTFontSymbolicTraits {
var stylisticClass: CTFontStylisticClass {
return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
}
}

func checkSansSerifness(fontName: String) {
let font = CTFontCreateWithName(fontName as CFString, 12, nil)
let fullName = CTFontCopyName(font, kCTFontFullNameKey)!
if CTFontGetSymbolicTraits(font).stylisticClass.contains(.sansSerifClass) {
print("(fullName) is sans serif.")
} else {
print("(fullName) is not sans serif.")
}
}

checkSansSerifness(fontName: "Helvetica")
checkSansSerifness(fontName: "Times New Roman")


Output:



Helvetica is sans serif.
Times New Roman is not sans serif.





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489197%2foption-sets-intersections-in-swift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    To understand these constants, let's look at the CTFontStylisticClass documentation:




    The class values are bundled in the upper four bits of the CTFontSymbolicTraits and can be obtained via kCTFontClassMaskTrait.




    To verify, let's look at the kCTFontClassMaskTrait documentation. If you set the language to Objective-C, the documentation shows the definitions of kCTFontClassMaskTrait:




    kCTFontClassMaskTrait = kCTFontTraitClassMask



    So it's just defined as another constant, which has all the same words in a different order. Ha ha, Apple, you're hilarious.



    Okay, let's look at the kCTFontTraitClassMask documentation. Again, if you set the language to Objective-C, you can see the definition of the constant:




    kCTFontTraitClassMask = (15U << kCTFontClassMaskShift)



    Indeed, 15U is four consecutive 1 bits, and it's shifted left by some amount. This is typical of a “mask”: it defines a subset of the bits in a binary word.



    To convert a CTFontSymbolicTraits to a CTFontStylisticClass, we need to use the mask to select just those bits from the CTFontSymbolicTraits raw value, and use the result as the raw value of a CTFontStylisticClass. We can do the selection by using the bitwise operator &, or by using the OptionSet method intersection.



    What we really want, in Swift, is a method on CTFontSymbolicTraits that extracts a CTFontStylisticClass. So let's write an extension:



    extension CTFontSymbolicTraits {
    var stylisticClass: CTFontStylisticClass {
    return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
    }
    }


    Let's test it:



    import CoreText
    import Foundation

    extension CTFontSymbolicTraits {
    var stylisticClass: CTFontStylisticClass {
    return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
    }
    }

    func checkSansSerifness(fontName: String) {
    let font = CTFontCreateWithName(fontName as CFString, 12, nil)
    let fullName = CTFontCopyName(font, kCTFontFullNameKey)!
    if CTFontGetSymbolicTraits(font).stylisticClass.contains(.sansSerifClass) {
    print("(fullName) is sans serif.")
    } else {
    print("(fullName) is not sans serif.")
    }
    }

    checkSansSerifness(fontName: "Helvetica")
    checkSansSerifness(fontName: "Times New Roman")


    Output:



    Helvetica is sans serif.
    Times New Roman is not sans serif.





    share|improve this answer






























      0














      To understand these constants, let's look at the CTFontStylisticClass documentation:




      The class values are bundled in the upper four bits of the CTFontSymbolicTraits and can be obtained via kCTFontClassMaskTrait.




      To verify, let's look at the kCTFontClassMaskTrait documentation. If you set the language to Objective-C, the documentation shows the definitions of kCTFontClassMaskTrait:




      kCTFontClassMaskTrait = kCTFontTraitClassMask



      So it's just defined as another constant, which has all the same words in a different order. Ha ha, Apple, you're hilarious.



      Okay, let's look at the kCTFontTraitClassMask documentation. Again, if you set the language to Objective-C, you can see the definition of the constant:




      kCTFontTraitClassMask = (15U << kCTFontClassMaskShift)



      Indeed, 15U is four consecutive 1 bits, and it's shifted left by some amount. This is typical of a “mask”: it defines a subset of the bits in a binary word.



      To convert a CTFontSymbolicTraits to a CTFontStylisticClass, we need to use the mask to select just those bits from the CTFontSymbolicTraits raw value, and use the result as the raw value of a CTFontStylisticClass. We can do the selection by using the bitwise operator &, or by using the OptionSet method intersection.



      What we really want, in Swift, is a method on CTFontSymbolicTraits that extracts a CTFontStylisticClass. So let's write an extension:



      extension CTFontSymbolicTraits {
      var stylisticClass: CTFontStylisticClass {
      return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
      }
      }


      Let's test it:



      import CoreText
      import Foundation

      extension CTFontSymbolicTraits {
      var stylisticClass: CTFontStylisticClass {
      return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
      }
      }

      func checkSansSerifness(fontName: String) {
      let font = CTFontCreateWithName(fontName as CFString, 12, nil)
      let fullName = CTFontCopyName(font, kCTFontFullNameKey)!
      if CTFontGetSymbolicTraits(font).stylisticClass.contains(.sansSerifClass) {
      print("(fullName) is sans serif.")
      } else {
      print("(fullName) is not sans serif.")
      }
      }

      checkSansSerifness(fontName: "Helvetica")
      checkSansSerifness(fontName: "Times New Roman")


      Output:



      Helvetica is sans serif.
      Times New Roman is not sans serif.





      share|improve this answer




























        0












        0








        0







        To understand these constants, let's look at the CTFontStylisticClass documentation:




        The class values are bundled in the upper four bits of the CTFontSymbolicTraits and can be obtained via kCTFontClassMaskTrait.




        To verify, let's look at the kCTFontClassMaskTrait documentation. If you set the language to Objective-C, the documentation shows the definitions of kCTFontClassMaskTrait:




        kCTFontClassMaskTrait = kCTFontTraitClassMask



        So it's just defined as another constant, which has all the same words in a different order. Ha ha, Apple, you're hilarious.



        Okay, let's look at the kCTFontTraitClassMask documentation. Again, if you set the language to Objective-C, you can see the definition of the constant:




        kCTFontTraitClassMask = (15U << kCTFontClassMaskShift)



        Indeed, 15U is four consecutive 1 bits, and it's shifted left by some amount. This is typical of a “mask”: it defines a subset of the bits in a binary word.



        To convert a CTFontSymbolicTraits to a CTFontStylisticClass, we need to use the mask to select just those bits from the CTFontSymbolicTraits raw value, and use the result as the raw value of a CTFontStylisticClass. We can do the selection by using the bitwise operator &, or by using the OptionSet method intersection.



        What we really want, in Swift, is a method on CTFontSymbolicTraits that extracts a CTFontStylisticClass. So let's write an extension:



        extension CTFontSymbolicTraits {
        var stylisticClass: CTFontStylisticClass {
        return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
        }
        }


        Let's test it:



        import CoreText
        import Foundation

        extension CTFontSymbolicTraits {
        var stylisticClass: CTFontStylisticClass {
        return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
        }
        }

        func checkSansSerifness(fontName: String) {
        let font = CTFontCreateWithName(fontName as CFString, 12, nil)
        let fullName = CTFontCopyName(font, kCTFontFullNameKey)!
        if CTFontGetSymbolicTraits(font).stylisticClass.contains(.sansSerifClass) {
        print("(fullName) is sans serif.")
        } else {
        print("(fullName) is not sans serif.")
        }
        }

        checkSansSerifness(fontName: "Helvetica")
        checkSansSerifness(fontName: "Times New Roman")


        Output:



        Helvetica is sans serif.
        Times New Roman is not sans serif.





        share|improve this answer















        To understand these constants, let's look at the CTFontStylisticClass documentation:




        The class values are bundled in the upper four bits of the CTFontSymbolicTraits and can be obtained via kCTFontClassMaskTrait.




        To verify, let's look at the kCTFontClassMaskTrait documentation. If you set the language to Objective-C, the documentation shows the definitions of kCTFontClassMaskTrait:




        kCTFontClassMaskTrait = kCTFontTraitClassMask



        So it's just defined as another constant, which has all the same words in a different order. Ha ha, Apple, you're hilarious.



        Okay, let's look at the kCTFontTraitClassMask documentation. Again, if you set the language to Objective-C, you can see the definition of the constant:




        kCTFontTraitClassMask = (15U << kCTFontClassMaskShift)



        Indeed, 15U is four consecutive 1 bits, and it's shifted left by some amount. This is typical of a “mask”: it defines a subset of the bits in a binary word.



        To convert a CTFontSymbolicTraits to a CTFontStylisticClass, we need to use the mask to select just those bits from the CTFontSymbolicTraits raw value, and use the result as the raw value of a CTFontStylisticClass. We can do the selection by using the bitwise operator &, or by using the OptionSet method intersection.



        What we really want, in Swift, is a method on CTFontSymbolicTraits that extracts a CTFontStylisticClass. So let's write an extension:



        extension CTFontSymbolicTraits {
        var stylisticClass: CTFontStylisticClass {
        return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
        }
        }


        Let's test it:



        import CoreText
        import Foundation

        extension CTFontSymbolicTraits {
        var stylisticClass: CTFontStylisticClass {
        return CTFontStylisticClass(rawValue: self.intersection(.classMaskTrait).rawValue)
        }
        }

        func checkSansSerifness(fontName: String) {
        let font = CTFontCreateWithName(fontName as CFString, 12, nil)
        let fullName = CTFontCopyName(font, kCTFontFullNameKey)!
        if CTFontGetSymbolicTraits(font).stylisticClass.contains(.sansSerifClass) {
        print("(fullName) is sans serif.")
        } else {
        print("(fullName) is not sans serif.")
        }
        }

        checkSansSerifness(fontName: "Helvetica")
        checkSansSerifness(fontName: "Times New Roman")


        Output:



        Helvetica is sans serif.
        Times New Roman is not sans serif.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 21:54

























        answered Nov 26 '18 at 21:47









        rob mayoffrob mayoff

        294k42592642




        294k42592642
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489197%2foption-sets-intersections-in-swift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks