andrewz
andrewz

Reputation: 5220

Total number of characters in a UIFont

Is there a way to query the total number of characters in a UIFont? For example, something like this: UIFont.systemFont(ofSize:16).count ? I am building an app to browse through all characters in a UIFont. I would like to know the total count of glyphs defined for that font, preferrably the maximum unicode code it supports.

Upvotes: 0

Views: 211

Answers (1)

KrishnaCA
KrishnaCA

Reputation: 5695

I believe you can achieve that by the following way using CoreText:

import CoreText

extension CharacterSet {
   func charCount() -> Int {
       var count = 0
       for plane:UInt8 in 0...16 where self.hasMember(inPlane: plane) {
          for unicode in UInt32(plane) << 16 ..< UInt32(plane+1) << 16 {
              if let uniChar = UnicodeScalar(unicode) {
                  if self.contains(uniChar) {
                     count += 1
                  }
              }
          }
       }
       return count
   }
}

func getCountForFont(font: UIFont) -> Int {

    let coreFont: CTFont = font
    let characterSet: CharacterSet = CTFontCopyCharacterSet(coreFont) as CharacterSet
    let count = characterSet.charCount()
    return count
}

Upvotes: 3

Related Questions