Search code examples
iosswiftfontsnsattributedstring

Console printing different text font swift


I have set different font for my textField and it is working fine. I can enter a gujarati font into my textField this way I set font:

enter image description here

But when I try to print user's text it's printing english letters. Suppose I enter into textField like અબચ but it prints Optional("abc") into console so I tried to use attributedString but it gives me same result and below is my code:

if let font = UIFont(name: "HARIKRISHNA", size: 16) {
        let attributedString = NSMutableAttributedString(string: userText!, attributes: [NSFontAttributeName : font])
        print(attributedString.string)
    } else {
        // The font "Raleway-SemiBold" is not found
        print("The font Raleway-SemiBold is not found")

    }

Is there any way to log output in same language of textField?

Sample for more Info.


Solution

  • The Xcode console window uses a font which can be changed in the Xcode preferences, but not programmatically at runtime. The print() function writes a "textual representation" of the given arguments. For a String, this is just the string itself:

    print(textF.text!)
    // Output:
    abc
    

    For an attributed string, the textual representation is a description containing the string and attributes:

    print(textF.attributedText!)
    // Output:
    abc{
        NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
        NSFont = "<UICTFont: 0x7fa6984241f0> font-family: \"HARIKRISHNA\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
        NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 2, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
        NSShadow = "NSShadow {0, -1} color = {(null)}";
    }
    

    Your actual problem seems to be a different one. Assigning a special font to a text field changes only the appearance of the string, not the characters in the string itself.

    So if the user enters "abc" then textF.text is "abc", even if you have a font which displays that as "અબચ". Therefore matching that string against "અબચ" cannot work.

    I do not have experience with the Gujarati language to propose a good solution. It might be interesting to know that you can "transliterate" foreign languages to the Latin alphabet (from http://nshipster.com/cfstringtransform/):

    let text = "અબચ" as NSMutableString
    CFStringTransform(text, nil, kCFStringTransformToLatin, false)
    print(text) 
    

    The result is "abaca" because

    અ = GUARATI LETTER A
    બ = GUARATI LETTER BA
    ચ = GUARATI LETTER CA
    

    But I don't know if that helps in your real application.