2

I have been following several Q/A aboit this: like this and this other one.

However I am unable to change the width of a UILabel.

Here is my code:

- (void)configureView {
    // Update the user interface for the detail item.
    if (self.detailItem) {
        Data *detailData = (Data*)self.detailItem;

        NSString * priceString = [NSString stringWithFormat:@"%@ %.2f", detailData.priceCurrency, detailData.priceAmount.floatValue ];
        CGSize size = [priceString sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
        CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
        _price.frame = CGRectMake(_price.frame.origin.x, _price.frame.origin.y, 300, adjustedSize.height);

        // I Tried this one as well..
        //_price.frame = CGRectMake(_price.frame.origin.x, _price.frame.origin.y, adjustedSize.width, adjustedSize.height);
        _price.text = priceString;

        // and this one 
        // [_price sizeToFit];

        // And this one
        [_price setFrame:CGRectMake(_price.frame.origin.x,_price.frame.origin.y,adjustedSize.width,adjustedSize.height)];
        [_price setText:priceString];

        // as well as hardocding the width to 300
        // [_price setFrame:CGRectMake(_price.frame.origin.x,_price.frame.origin.y,300,adjustedSize.height)];
    }
}

Unfortunately none of the above changes the width UILabel.

This is how I set it up in Interface builder, is there something wrong here? The original width is 40 so by hardcoding it to 300 I expected it to change.

enter image description here

The view is a DetailView which is accessed from a MasterViewController when the user taps on a cel. This is the code on DetailViewController that I use to call the configureView method described above.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

Any idea on what I could be missing?

This is how I initialize my outlets:

// DetailViewController.m 
@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize price = _price;

The good thing is that the text content is there so the outlet references are not nil.

However this is how the UILabel appears:

enter image description here

EDIT:

I tried to remove the constraints but it seems to be still not effective.

This is the code now:

   _price.frame = CGRectMake(_price.frame.origin.x, _price.frame.origin.y, 300, 200);
        _price.text = @"bdadadsdsdsdsDsds";

enter image description here

Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170
  • 1
    I believe the constraints are the problem. – rmaddy Feb 12 '15 at 18:29
  • I tried to change them but I am unsure on which one I should be changing.. any suggestion? – mm24 Feb 12 '15 at 18:35
  • Did you set the width constraint? In the IB, see if you have a constraint with 40 in it. – yusuke024 Feb 12 '15 at 18:45
  • BTW, comment out this line: `[_price setFrame:CGRectMake(_price.frame.origin.x,_price.frame.origin.y,adjustedSize.width,adjustedSize.height)];` and see if anything changes. – yusuke024 Feb 12 '15 at 18:48
  • I did. See my EDIT picture. I also clicked on "clear all constraints". I tried to add a long string to the price label but would't do the job.. – mm24 Feb 12 '15 at 18:49
  • Setting the frame manually should not be done at all when your view has constraints on it. You must either set the width using just constraints, or set the position and size with just `frame`. – mattsven Feb 12 '15 at 18:53

1 Answers1

0

The viewDidLayoutSubviews is the most appropriate place to modify the UI before it actually appears on the screen. Try to move your code to viewDidLayoutSubviews.

Hope this helps

Sarim Sidd
  • 2,166
  • 2
  • 22
  • 31