Attributed string with custom fonts in storyboard

2019-03-07 20:46发布

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 all revert to Helvetica when running on simulator or device, although they look all right in storyboard.

I'm not sure if it's a bug of Xcode 6 or iOS 8 SDK, or the way to use custom fonts is changed in Xcode 6 / iOS 8?

14条回答
▲ chillily
2楼-- · 2019-03-07 21:11

Met the same problem: the attribute font set for TextView in storyboard didn't work in run time with XCode 6.1 & iOS 8 SDK.

This is how I solved this issue, might be a workaround for you:

  1. open the attribute inspector of your textview, change text to "Plain"

  2. click on the cross to delete the "wC hR"(red below)

    enter image description here

  3. change text to "Attributed", and then you can set the font and size for your text.

  4. run to check if it works
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-07 21:12

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 ☝️

查看更多
\"骚年 ilove
4楼-- · 2019-03-07 21:16

Double click and install the font to the system. It will work (Xcode 8.2)

查看更多
男人必须洒脱
5楼-- · 2019-03-07 21:21

In case of attributed string you can add custom font in font list as - Click on font icon this will display following dialog .In the following dialog you can add your own category or existing one for custom font.attributed font dialog

After it click on Manage Fonts it open the following dialog select category in you created or existing one . Click on + sign to add font in the category. Manage font dialog

查看更多
走好不送
6楼-- · 2019-03-07 21:22

The fix for me was to use an IBDesignable class:

import UIKit

@IBDesignable class TIFAttributedLabel: UILabel {

    @IBInspectable var fontSize: CGFloat = 13.0

    @IBInspectable var fontFamily: String = "DIN Light"

    override func awakeFromNib() {
        var attrString = NSMutableAttributedString(attributedString: self.attributedText)
        attrString.addAttribute(NSFontAttributeName, value: UIFont(name: self.fontFamily, size: self.fontSize)!, range: NSMakeRange(0, attrString.length))
        self.attributedText = attrString
    }
}

Giving you this in the Interface Builder:

Interface Builder custom font with attributed string

You can set up your attributedstring just as you normal do, but you'll have to set your fontsize and fontfamily once again in the new available properties.

As the Interface Builder is working with the custom font by default, this results in a what you see is what you get, which I prefer when building apps.

Note

The reason I'm using this instead of just the plain version is that I'm setting properties on the attributed label like the linespacing, which are not available when using the plain style.

查看更多
聊天终结者
7楼-- · 2019-03-07 21:22

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

查看更多
登录 后发表回答