Custom Font Sizing in Xcode 6 Size Classes not working properly with Custom Fonts

前端 未结 14 1169
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 17:42

Xcode 6 has a new feature where fonts and font sizes in UILabel, UITextField, and UIButton can be s

相关标签:
14条回答
  • 2020-11-29 18:37

    I've come up with even faster fix (I assume you always use your one custom font).

    Create a category for UILabel and include in files using buggy storyboard with size classes and dedicated font setting for various classes:

    @implementation UILabel (FontFixForSizeClassesAppleBug)
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        if([[UIFont systemFontOfSize:10].familyName isEqualToString:self.font.familyName]) {
            //workaround for interface builder size classes bug which ignores custom font if various classes defined for label: http://stackoverflow.com/questions/26166737/custom-font-sizing-in-xcode6-size-classes-not-working-properly-w-custom-fonts
            self.font = [UIFont fontWithName:@"YOUR_CUSTOM_FONT_NAME" size:self.font.pointSize];
        }
    }
    
    @end
    

    Just use your custom fonts in storyboard. When buggy interpreter will use system font instead your own this category will switch it to your custom font.

    0 讨论(0)
  • 2020-11-29 18:38

    I had the same problem and found one not really clear, but fine solution!

    1. First you set the required font size in storyboard with system font name.
    2. Then to this label you assign a tag from 100 to 110 (or more, but 10 was always enough for me in one view controller).
    3. Then put this code to your VC's source file and don't forget to change font name. Code in swift.
    override func viewDidLayoutSubviews() {
        for i in 100...110 {
            if let label = view.viewWithTag(i) as? UILabel {
                label.font = UIFont(name: "RotondaC-Bold", size: label.font.pointSize)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:41

    Similar solution to @razor28's one but I think a little more universal. Also, works fine on older iOS versions

    https://gist.github.com/softmaxsg/8a3ce30331f9f07f023e

    0 讨论(0)
  • 2020-11-29 18:42

    Workaround for UILabel: keep the same font size on all Size Classes, but instead change your label height accordingly in each Size Class. Your label must have autoshrink enabled. It worked nicely in my case.

    0 讨论(0)
  • 2020-11-29 18:42

    This (and other Xcode - Size Classes related) bug caused me some serious grief recently as I had to go through a huge storyboard file hacking things away.

    For anyone else in this position, I'd like to add something on top of @razor28's answer to ease the pain.

    In the header file of your custom subclass, use IBInspectable for your runtime attributes. This will make these attributes accessible from the "Attributes Inspector", visually right above the default position for font settings.

    Example use:

    @interface MyCustomLabel : UILabel
    
        @property (nonatomic) IBInspectable NSString *fontType;
        @property (nonatomic) IBInspectable CGFloat iphoneFontSize;
        @property (nonatomic) IBInspectable CGFloat ipadFontSize;
    
    @end
    

    This will very helpfully produce this output:

    enter image description here

    An added benefit is that now we don't have to add the runtime attributes manually for each label. This is the closest I could get to XCode's intended behaviour. Hopefully a proper fix is on its way with iOS 9 this summer.

    0 讨论(0)
  • 2020-11-29 18:46

    The problem is still there that you cannot use the feature to set Fonts for different size classes from interface builder.

    Just set font based on the device you want just like below:

    if (Your Device is iPAd) //For iPad
    {
       [yourLabel setFont:[UIFont fontWithName:@"FontName" size:FontSize]]; 
    }
    else  //For Other Devices then iPad
    {
       [yourLabel setFont:[UIFont fontWithName:@"FontName" size:FontSize]]; 
    }
    

    This works perfectly on all devices.

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