UIAppearance not taking effect on UILabels created programmatically

前端 未结 5 1950
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 07:34

We have extended UILabel to be able to apply standard fonts and colors for all uses of a given label type in our apps. Eg.

@interface UILabelHeadingBold : UILab         


        
5条回答
  •  执念已碎
    2021-02-07 08:16

    @robert.wijas solution works great !

    For iOS 7 and upwards I had to update the key since the one he used are deprecated for 7+ :

    - (void)setTextAttributes:(NSDictionary *)numberTextAttributes;
    {
        UIFont *font = [numberTextAttributes objectForKey:NSFontAttributeName];
        if (font) {
            self.font = font;
        }
        UIColor *textColor = [numberTextAttributes objectForKey:NSForegroundColorAttributeName];
        if (textColor) {
            self.textColor = textColor;
        }
        UIColor *textShadowColor = [numberTextAttributes objectForKey:NSShadowAttributeName];
        if (textShadowColor) {
            self.shadowColor = textShadowColor;
        }
        NSValue *shadowOffsetValue = [numberTextAttributes objectForKey:NSShadowAttributeName];
        if (shadowOffsetValue) {
            UIOffset shadowOffset = [shadowOffsetValue UIOffsetValue];
            self.shadowOffset = CGSizeMake(shadowOffset.horizontal, shadowOffset.vertical);
        }
    }
    

提交回复
热议问题