B Masri
B Masri

Reputation: 33

How to display custom characters in custom font from 0xFF01 to 0xFF71 in swift

I used the following swift code to display special characters in custom uifont. However; the code works on iPhone 13 Pro Max simulator but does not work on real device iPhone 13 Pro Max. On the simulator the special characters are displayed correctly and the special character are displayed (0xFF01 to 0xFF71), but on the device I get different characters (like P, D, etc). The font added to Xcode, even when I step through the code I can see the special font is correctly selected using UIFont().


ch = 0xFF01

currentFont = UIFont(name: String(cString: fontNamePtr), size:CGFloat(fntSize))! 

currentFontAttributes = [NSAttributedString.Key.font: currentFont] 

let charStr = String(Character(UnicodeScalar(ch)!)) 

charStr.draw(at: CGPoint(x:CGFloat(x), y: CGFloat(y)), withAttributes: currentFontAttributes)

Please any ideas?

Upvotes: 0

Views: 258

Answers (1)

Scott Thompson
Scott Thompson

Reputation: 23701

When your code is running in the Simulator, it is actually running on the MacOS and has access to all the Fonts on your desktop machine. When it is running on the Phone, it only has access to the fonts on the phone.

When searching for a glyph for a particular code sequence, if the system can't find a glyph in the current font, it can consult a chain of "fallback" fonts to see if some other font can provide the glyph. If something in the fallback chain has a glyph, the system can draw that.

You may be using a font that does not have a glyph for the code sequence in your string. When running on MacOS, in the simulator, the OS is able to find a fallback font that provides a glyph - and draws it.

On the Phone, the system may not have a font with that glyph, or it may find a glyph in a different font than what it finds on the MacOS so it draws nothing, or the "wrong" thing.

Upvotes: 2

Related Questions