1

我有可用填充的问题。我想让我的 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 只是“-”时宽度没有减小?

4

1 回答 1

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 然后将其设置为 7 in 的方法中layoutSubviews。为什么?

于 2012-10-18T05:25:33.007 回答