Question

i've problems with uilable padding. i want to make my uilabel have padding event just one character display

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

so i have padding from "Ask Question" words,

what i have done is:

#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

i put like this

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

but why when i label.text just " - " the width not decrease ?

Was it helpful?

Solution

This is confusing. In layoutSubviews you try to set the new width of the label to the label's current x origin. That makes no sense at all. You should be setting the label's width based on the text in the label plus whatever padding you wish.

In layoutSubviews, replace this line:

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

with this:

[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;

Another strange thing in your code - in the init method you set the label's corner radius to 20 then you set it to 7 in layoutSubviews. Why?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top