0

我有两个按钮,每个按钮的左侧都有一个徽章标签,但我希望这些徽章标签的大小是动态的,我现在该怎么做是这样的 当我启动应用程序时

当我启动应用程序时

但是当我向上滚动孔单元时,它应该是这样的应该如何

使用 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);
}

谢谢。

4

2 回答 2

0

您可以尝试-sizeToFit,它应该可以正常工作,我要添加的一件事是手动为空文本值设置框架。另一件事是您可以使用superview属性更好地更新标签位置,这是草稿(不是覆盖-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);
}
于 2013-01-16T13:28:46.553 回答
0

我自己找到了一些解决方案,我现在在 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];
于 2013-01-17T11:09:05.630 回答