UIAppearance not taking effect on UILabels created programmatically

前端 未结 5 1945
佛祖请我去吃肉
佛祖请我去吃肉 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:34

    Simple hack that is working for me with no issues is to create a category with a UIAppearance setter that modifies UILabel properties.

    Following UIAppearance conventions I created a method:

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

    In UILabel category:

    @interface UILabel (UISS)
    
    - (void)setTextAttributes:(NSDictionary *)numberTextAttributes UI_APPEARANCE_SELECTOR;
    
    @end
    

    I'm still trying to figure out why the original setter does not work.

提交回复
热议问题