Search code examples
xcodecustom-font

Xcode Custom Font applied On UITextView, Not showing but if User type anything there, the User Text shows with Custom Font


I used some Custom Fonts all over my app and I know how to use them.

I have some simple StoryBoard pages like Contact Us and etc... they doesn't have any related Classes or Swift files. They are just simple text's.

When I set Custom Font for their UITextView. On the Xcode StoryBoard, the Font shows fine. But on Simulator or phone. They are showing with default fonts.

The interesting part is when I check the editable option for this Fonts, and trying to change the text on the phone, the new text comes with the current Custom Font! But the original text is still at default font!


Solution

  • Maybe my issue is the same with yours. I also use custom font for UILabel on my .xib file. If I choose Text type is "Plain", they works well. But when I change to "Attributed" text, the UILabel show default font although my custom font is also set. My solution is set custom font by codes. Here is the code I set custom font (with attributed text):

    UIFont *font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
    NSDictionary *attrDict = @{
                               NSFontAttributeName : font,
                               NSForegroundColorAttributeName : [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1]
                               };
    
    NSMutableAttributedString *aAttrString = [[NSMutableAttributedString alloc] initWithString:_aboutTeacher.text attributes: attrDict];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:3.0f];
    [style setAlignment:NSTextAlignmentCenter];
    
    [aAttrString addAttribute:NSParagraphStyleAttributeName
                        value:style
                        range:NSMakeRange(0, [_aboutTeacher.text length])];
    
    _aboutTeacher.attributedText = aAttrString;