0

I'm trying to make custom badge. For that I make subclass of UIButton and remake - (void)drawRect:(CGRect)rect like this:

    - (void)drawRect:(CGRect)rect
    {
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.userInteractionEnabled = NO;
        self.layer.borderWidth = 1.5f;
        self.layer.cornerRadius = rect.size.width / 2;
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
        self.layer.backgroundColor = [UIColor grayColor].CGColor;
        self.layer.borderColor = [UIColor grayColor].CGColor;
    }

And everything is good except alignment. It is a little upper than it should be. Looks like this: enter image description here

What to do?

Maria
  • 755
  • 1
  • 11
  • 29
  • Try adding line [self.titleLabel setCenter:self.center]; – channi Feb 18 '14 at 12:42
  • 1
    @maria: You want that badge to be shown in tab of tab bar controller? @property(nonatomic,copy) NSString *badgeValue; // default is nil It's a property of tabbar item – Rajesh Feb 18 '14 at 12:47
  • I tried the exact code, and it's working perfectly. – Midhun MP Feb 18 '14 at 12:49
  • You could use this library. its pretty neat for badges.. [iOS Badges](https://github.com/jessesquires/JSCustomBadge) – Ahmed Z. Feb 18 '14 at 12:58

2 Answers2

0

@property(nonatomic) NSTextAlignment textAlignment;
// default is NSTextAlignmentLeft

It's an attribute of UILabel.

Omer Obaid
  • 416
  • 1
  • 6
  • 24
Nicolas Bonnet
  • 1,275
  • 11
  • 15
0

Here is badge I need: https://github.com/jessesquires/JSCustomBadge . Works perfect!

Maria
  • 755
  • 1
  • 11
  • 29