3

I am developing an iOS app in Swift 3. In this app I am listing all available fonts (system provided) but I would like to list all available characters for them too.

For example I am using Font Awesome to and I want the user to be able to select any of the characters/symbols from a list. How can I do this?

This is how I get an array of the fonts. How can I get an array of all characters for a selected font?

UIFont.familyNames.map({ UIFont.fontNames(forFamilyName: $0)}).reduce([]) { $0 + $1 }
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

2 Answers2

6

For each UIFont, you have to get characterSet of that font. For example, I take first UIFont.

let firsttFont = UIFont.familyNames.first

let first = UIFont(name: firsttFont!, size: 14)
let fontDescriptor = first!.fontDescriptor
let characterSet : NSCharacterSet = fontDescriptor.object(forKey: UIFontDescriptorCharacterSetAttribute) as! NSCharacterSet

Then, use this extension to get all characters of that NSCharacterSet:

extension NSCharacterSet {
    var characters:[String] {
        var chars = [String]()
        for plane:UInt8 in 0...16 {
            if self.hasMemberInPlane(plane) {
                let p0 = UInt32(plane) << 16
                let p1 = (UInt32(plane) + 1) << 16
                for c:UTF32Char in p0..<p1 {
                    if self.longCharacterIsMember(c) {
                        var c1 = c.littleEndian
                        let s = NSString(bytes: &c1, length: 4, encoding: String.Encoding.utf32LittleEndian.rawValue)!
                        chars.append(String(s))
                    }
                }
            }
        }
        return chars
    }
}

(Ref: NSArray from NSCharacterset)

So, at last, just call characterSet.characters to get all characters (in String)

Community
  • 1
  • 1
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • Please add a link to the original if you use code from other sources. Your extension method seems to be a verbatim copy of http://stackoverflow.com/a/34772440/1187415 which in turn is based on http://stackoverflow.com/a/15742659/1187415. – Martin R Jan 11 '17 at 14:14
  • Yes, I've added the original of this extension. – Duyen-Hoa Jan 11 '17 at 14:31
1

I don't think you'll be able to without a lot of coding. Here's a few links to Apple documentation:

In their main font page you'll have to scroll down a bit to get to a list of documentation, but in that list is their TrueType reference manual. The characters are stored as glyphs, meaning they are vector-based to allow for clean font sizes. (I believe the simple drop-down of font sizes in IB are merely "suggestions", and you can type in any size you care to.)

In that second link, scroll down to the lengthy list of font tables. One looks promising - the cmap table. But reading through this, it's possible to (a) have foreign characters like "umlaut A" or Chinese and (b) omit characters in each font. Also, this is just a lookup table - you'll then maybe need to use the mapping table to get the location of the glyph.

If you are targeting English only, you might be better off finding a way to check if the letters "Aa" exist for the font and display them.