Detecting when a Unicode character cannot be displayed correctly

Some Unicode characters cannot be displayed on iOS, but are displayed correctly on macOS. Similarly, some Unicode characters that iOS can display cannot be displayed on watchOS. This is due to the fact that different built-in fonts are installed on these platforms.

When a character cannot be displayed, is it displayed as? inside the box like this:
enter image description here

I also saw some characters appear as aliens (I don't know why the difference):
enter image description here

Is there a way to find out when a particular Unicode character will not display properly given a Unicode character string such as "α„₯"?

I need a solution that works for both iOS and watchOS.

+5
2

CTFontGetGlyphsForCharacters(), , ( , ):

CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), 12, NULL);
const UniChar code_point[] = { 0xD83C, 0xDCA1 };  // U+1F0A1
CGGlyph glyph[] = { 0, 0 };
bool has_glyph = CTFontGetGlyphsForCharacters(font, code_point, glyph, 2);

Swift:

let font = CTFontCreateWithName("Helvetica", 12, nil)
var code_point: [UniChar] = [0xD83C, 0xDCA1]
var glyphs: [CGGlyph] = [0, 0]
let has_glyph = CTFontGetGlyphsForCharacters(font, &code_point, &glyph, 2)

, , , CTFontCopyDefaultCascadeListForLanguages(). , .

+4

undefined U+1FFF:

/// - Parameter font: a UIFont
/// - Returns: true if glyph exists
func glyphAvailable(forFont font:UIFont) -> Bool {
    if let refUnicodePng = Character("\u{1fff}").png(forFont: font),
        let myPng = self.png(forFont: font) {
        return refUnicodePng != myPng
    }
    return false
}

png:

/// - Parameter font: a UIFont
/// - Returns: an optional png representation
func png(forFont font: UIFont) -> Data? {
    let attributes = [NSAttributedStringKey.font: font]
    let charStr = "\(self)" as NSString
    let size = charStr.size(withAttributes: attributes)

    UIGraphicsBeginImageContext(size)
    charStr.draw(at: CGPoint(x: 0,y :0), withAttributes: attributes)

    var png:Data? = nil
    if let charImage = UIGraphicsGetImageFromCurrentImageContext() {
        png = UIImagePNGRepresentation(charImage)
    }

    UIGraphicsEndImageContext()
    return png
}

.

0

All Articles