Attributed string with custom fonts in storyboard does not load correctly

后端 未结 16 1790
梦谈多话
梦谈多话 2021-01-30 03:35

We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard

相关标签:
16条回答
  • 2021-01-30 04:05

    For anyone applying custom fonts to attributed string in code: Try setting it in viewDidLayoutSubviews. My mistake was doing it in viewDidLoad, it won't be applied there.

    0 讨论(0)
  • 2021-01-30 04:08

    The same problem.

    Solved: Just check Selectable in TextView. Without this i have standard System font.

    0 讨论(0)
  • 2021-01-30 04:10

    I have struggled with this bug: UILabel displays correctly in IB with custom font but does not display correctly on device or simulator (font is included in the project and is used in plain UILabels).

    Finally found Attributed String Creator on (Mac) App Store. Generates code to be placed in your app in the appropriate place. Fantastic. I am not the creator, just a happy user.

    0 讨论(0)
  • 2021-01-30 04:10

    that's have a simple and quick solition and that's work in my case . that solution is add a code line in didFinishLaunchingWithOptions func in AppDelegate.swift file :

    for textViews :

    UITextView.appearance().font = UIFont(name: "IranSans", size: 17)
    

    for labels :

    UILabel.appearance().font = UIFont(name: "IranSans", size: 17)
    

    and for rest of UiView like this two ☝️

    0 讨论(0)
  • 2021-01-30 04:12

    My solution is a bit of a work around. The real solution is for apple to fix Interface Builder.

    With it you can mark all the bold and italic text in interface builder using a system font, then at runtime render your custom font. May not be optimal in all cases.

     NSMutableAttributedString* ApplyCustomFont(NSAttributedString *attributedText,
                         UIFont* boldFont,
                         UIFont* italicFont,
                         UIFont* boldItalicFont,
                         UIFont* regularFont)
    {
    
        NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc] initWithAttributedString:attributedText];
        [attrib beginEditing];
        [attrib enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrib.length) options:0
                        usingBlock:^(id value, NSRange range, BOOL *stop)
        {
            if (value)
            {
                UIFont *oldFont = (UIFont *)value;
                NSLog(@"%@",oldFont.fontName);
    
                [attrib removeAttribute:NSFontAttributeName range:range];
    
                if([oldFont.fontName rangeOfString:@"BoldItalic"].location != NSNotFound && boldItalicFont != nil)
                    [attrib addAttribute:NSFontAttributeName value:boldItalicFont range:range];
                else if([oldFont.fontName rangeOfString:@"Italic"].location != NSNotFound && italicFont != nil)
                    [attrib addAttribute:NSFontAttributeName value:italicFont range:range];
                else if([oldFont.fontName rangeOfString:@"Bold"].location != NSNotFound && boldFont != nil)
                    [attrib addAttribute:NSFontAttributeName value:boldFont range:range];
                else if(regularFont != nil)
                    [attrib addAttribute:NSFontAttributeName value:regularFont range:range];
            }
        }];
        [attrib endEditing];
    
        return attrib;
    }
    

    Inspired by this post

    0 讨论(0)
  • 2021-01-30 04:13

    The simplest answer that worked for is to drag the fonts into FontBook. If the fonts are in your project but not in your computer's FontBook, IB sometimes has trouble finding it. Weird, but has worked for me on several occasions.

    0 讨论(0)
提交回复
热议问题