Problems with BadgeValue text color when overwrites UILabel

If you overwrite the UILabel in Objective C, for example to get the functionality of VerticalAlign, like I wanted to do, You can get bad side effects.

I used a small code part I found on Internet, to get this functions.

@implementation UILabel (VerticalAlign)
- (void)alignTop {
    CGSize fontSize = [self.text sizeWithFont:self.font];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text sizeWithFont:self.font  constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [self.text stringByAppendingString:@"\n "];
}
 
- (void)alignBottom {
    CGSize fontSize = [self.text sizeWithFont:self.font];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
 
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
}
 
-(id)initWithFrame:(CGRect)frame {
    id result = [super initWithFrame:frame];
    if (result) {
        [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
    }
    return result;
}
 
@end

The problem was, that now the text color of the badge value was gray, where in documentation from apple was described, that is absolutely not possible to change the white color of this value. But it was also not possible to change it to a different color than gray (160.0, 160.0, 160.0, 1.0).

After a lot of searching, trying and crying, I found the solution:

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
}
 
-(id)initWithFrame:(CGRect)frame {
    id result = [super initWithFrame:frame];
    if (result) {
        [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
    }
    return result;
}

These two methods must be removed from code, then everything works well and the badge value has its white color again back.

On this place a lot of thanks to the guys and girls from stackoverflow, who helped me to find the solution.

http://stackoverflow.com/questions/13143646/uitabbaritem-badgevalue-text-color/

 

Print Friendly

One thought on “Problems with BadgeValue text color when overwrites UILabel

  1. Agentur Typo3 Programmierung at Steilstarter

    This is the perfect solution in overwrites UILabel that I can say because it’s so difficult to understand this problem.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *