0

I have tow buttons and on the left side of every button is a badge label but i want that the size of these badge label dynamic how can i do this at the moment is something like that when i start the app

When i start the app

but when i scroll the hole cell up then it is how it should be how it should be

Using Iphone sdk 6.0:

#import <QuartzCore/QuartzCore.h>

 #import "JSMBadgeLabel.h"

 @interface JSMBadgeLabel ()

 -(void) setup;
 @end

 @implementation JSMBadgeLabel

 @synthesize targetUIView = _targetUIView;

 -(void) setup{
    self.layer.cornerRadius = self.font.pointSize *0.8;

 }

 -(void)awakeFromNib
 {
    [self setup];
 }

 -(void)setText:(NSString *)text{
    [super setText:text];

    CGSize size = [text sizeWithFont:self.font];
    self.frame = CGRectMake(self.frame.origin.x ,self.frame.origin.y,size.width + self.font.pointSize *0.8,size.height);
    CGRect badgeFrame = self.frame;
    CGRect targetFrame = self.targetUIView.frame;

    self.frame = CGRectMake(targetFrame.origin.x+targetFrame.size.width- badgeFrame.size.width/2,targetFrame.origin.y-badgeFrame.size.height/2,badgeFrame.size.width,badgeFrame.size.height);}@end
-(void) updateAsBadge:(NSString*)newText {
    self.text = newText;
    [self sizeToFit];
    const int padding = 1;
    self.frame = CGRectMake(self.superview.bounds.size.width - self.bounds.size.width-padding, padding, self.bounds.size.width, self.bounds.size.height);
}

Thanks.

Mingebag
  • 748
  • 2
  • 20
  • 35
  • possible duplicate of [How to calculate UILabel width based on text length?](http://stackoverflow.com/questions/3527494/how-to-calculate-uilabel-width-based-on-text-length) – tilo Jan 16 '13 at 12:56
  • Make sure you've changed "Citys" to "Cities" :) @tilo it seems not to be the duplicate as the frame is set correctly after the cell is redrawn, there's also a logic of calculating the label size which looks fine. – A-Live Jan 16 '13 at 13:18
  • @A-Live haha thx still dont work ;) – Mingebag Jan 16 '13 at 13:20

2 Answers2

0

You could try -sizeToFit, it should work fine, one thing I would add is to set the frame manually for an empty text value. Another thing is that you can update the label position better with a superview property, here's a draft (not overriding -setText:)

-(void) updateAsBadge:(NSString*)newText {
  self.text = newText;
  [self sizeToFit];
  const int padding = 1;
  self.frame = CGRectMake(self.superview.width - self.width-padding, padding, self.width, self.height);
}
A-Live
  • 8,904
  • 2
  • 39
  • 74
  • Note that's more about optimization than solving the problem. – A-Live Jan 16 '13 at 13:33
  • Property width was not found on object of type "JSMBadeLabel". im sry can you repost my code with your changes somehow i dont see what you have done ... – Mingebag Jan 16 '13 at 14:18
  • @Mingebag You'll have to access `width` from `.bounds.size`, I used to have an utility class-extension for an easier access. – A-Live Jan 16 '13 at 14:43
  • @Mingebag yeah, `[self sizeToFit]` should resize the label and `self.frame =...` moves it to the top-right corner of the superview. – A-Live Jan 16 '13 at 15:05
  • nothing happens i have edit my code at the top but still the same problem – Mingebag Jan 16 '13 at 15:05
  • @Mingebag could you share the piece of code where you set the label text by calling `-updateAsBadge:` or overridden `-setText:` ? – A-Live Jan 16 '13 at 15:12
  • cell.villagesBadge.text =[NSString stringWithFormat:@"%d",city.unterort.count]; that is how i set the text in the cell – Mingebag Jan 16 '13 at 15:15
0

i found some solution by my self im now including this badge in the TablViewController

cell.thingsBadge.text =[NSString stringWithFormat:@"%d",city.unterort.count];
CGSize size1 = [cell.villagesBadge.text sizeWithFont:cell.villagesBadge.font];
CGRect newFrame1 = CGRectMake(cell.villagesBadge.frame.origin.x+(cell.villagesBadge.frame.size.width - (size1.width+4)), cell.villagesBadge.frame.origin.y, size1.width+4, cell.thingsBadge.frame.size.height);
cell.thingsBadge.layer.cornerRadius = cell.villagesBadge.font.pointSize *0.8;
cell.thingsBadge.frame = newFrame1;
[cell.thingsBadge setNeedsDisplay];
Mingebag
  • 748
  • 2
  • 20
  • 35