9

I have not exact idea for it is possible or not and also I did try to find solution for my question but did't get success, that's why I am asking here and hope that I will get solution.

I am developing one drawing related app in which I want to get shape of each font that provided by .ttf file or UIFont.

For Ex.

If I have UIFont/.ttf file such like "OpenSans-Italic" then how can get each "char" with it's shape (here "Italic" shape) of "OpenSans" font? And I want to this in Char or NSString.

=> @[ @"a", @"b", @"c", @"d",..@"A", @"B", @"C", @"D",.....@"1", @"2", @"3"....]; // This is char array only for understanding purpose.

Please suggest me, how can I get this?

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Do you want each character as an [attributed string](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/) or as an image? –  Jan 04 '16 at 13:44
  • @KennethBruno Not image I want each char as string. – iPatel Jan 04 '16 at 13:46
  • Maybe this is help: http://stackoverflow.com/a/6991793/2754158 looks like the code there gets font character set. – Krzysztof Jan 12 '16 at 09:12
  • 1
    Instead of `UIFont` or `CGFont`, you should probably go for `CTFont`. Core Text is the only framework that will let you get the shape (path) of a glyph (note that a font doesn't represent characters, it represents glyphs. A character can be composed from multiple glyphs, e.g. characters with diacritics, and character sequences can be represented with one glyph, e.g. ligatures). – Sulthan Jan 12 '16 at 09:19

2 Answers2

3

Here's a way to do it, borrowing from this post. It iterates through all the possible characters in a NSCharacterSet and finds those that are valid. Right now it's using .URLPathAllowedCharacterSet() but it could use others.

import UIKit

var attributedString = NSMutableAttributedString()
if let font = UIFont(name: "OpenSans-Italic", size: 12) {
  let charset = NSCharacterSet.URLPathAllowedCharacterSet()
  for var plane : UInt32 in 0...16 where charset.hasMemberInPlane(UInt8(plane)) {
    let planeRange = (plane << 16)..<((plane+1) << 16)
    for characterValue in planeRange where charset.longCharacterIsMember(characterValue) {
      let character = String(UnicodeScalar(CFSwapInt32HostToLittle(characterValue)))
      attributedString.appendAttributedString(NSAttributedString(string:character, attributes: [NSFontAttributeName:font]))
    }
  }
}
Community
  • 1
  • 1
  • Thanks for give answer, can you please convert this code in Objective-C ? – iPatel Jan 05 '16 at 06:17
  • 1
    @iPatel Your question lists Swift as the language that you are using. If you go to the link I posted there is similar Objective-C code in one of the answers. –  Jan 05 '16 at 06:21
0

Is it work if available glyphs are listed in code? If it works, everything seems easy.

I mean add some build phase to parse font file and generate some code including all available chars. I have created a project that provided similar idea IconFont. This project provide UIImage from font file. In develop branch, I add a build phase to parse ttf font file using project PyTTF and add code to TBCityIcon.m. It is easy to modify the python script to generate code of a NSArray that contains all available chars.

John Wong
  • 342
  • 2
  • 10