2012-10-18 60 views
1

我有使用uilable填充的问题。我想我的UILabel有填充事件只是一个字符显示基于内容的UILabel填充

http://i.stack.imgur.com/T4Aet.png

所以我必须从“提问”字样填充,

什么,我所做的是:

#import "NetraCell.h" 
#import <QuartzCore/QuartzCore.h> 
@implementation NetraCell 
@synthesize NetraLabelForPrice,NetraImageDeals; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     /////implement for NetraLabelForPrice 
// 
     NetraLabelForPrice=[[UILabel alloc] init]; 
     NetraLabelForPrice.backgroundColor=[UIColor brownColor]; 
     NetraLabelForPrice.textColor=[UIColor whiteColor]; 
     NetraLabelForPrice.textAlignment=NSTextAlignmentCenter; 
     NetraLabelForPrice.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 
     [NetraLabelForPrice.layer setCornerRadius:20]; 


    // NetraImageDeals=[[UIImage alloc] init]; 
    [self.contentView addSubview:NetraLabelForPrice]; 
     ///// 


    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 
- (void)layoutSubviews { 

    [super layoutSubviews]; 
    self.imageView.frame = CGRectMake(10.0f, 15.0f, 80.0f, 80.0f); 
    self.NetraLabelForPrice.frame = CGRectMake(100.0f, 15.0f, NetraLabelForPrice.frame.origin.x, 25); 
    [self.imageView.layer setCornerRadius:7.0f]; 
    [self.NetraLabelForPrice.layer setCornerRadius:7.0f]; 

} 
@end 

我把这样的

self.NetraLabelForPrice.frame = CGRectMake(100.0f, 15.0f, NetraLabelForPrice.frame.origin.x, 25); 

但为什么当我label.text j ust“ - ”宽度不减?

回答

1

这很混乱。在layoutSubviews中,您尝试将标签的新宽度设置为标签的当前x原点。这根本没有意义。您应该根据标签中的文本以及您希望的任何填充来设置标签的宽度。

layoutSubviews,替换此行:

self.NetraLabelForPrice.frame = CGRectMake(100.0f, 15.0f, NetraLabelForPrice.frame.origin.x, 25); 

与此:

[NetraLabelForPrice sizeToFit]; // make the label just big enough for the text 
CGRect labelFrame = NetraLabelForPrice.frame; 
labelFrame.origin.x = 100; 
labelFrame.origin.y = 15; 
labelFrame.size.width += 20; // set to the amount of padding you want 
NetraLabelForPrice.frame = labelFrame; 

代码中的另一个奇怪的事情 - 在init方法,你标签的圆角半径设置为20,那么你设置它在layoutSubviews中为7。为什么?