0

I feel like this is a very simple question- I've searched high and low for an answer and came up with nothing. In my UITableViewController I set the text of a UILabel of my custom UITableViewCell (made in the storyboard). I want the width of the label to be the width of the text (which is not that difficult to calculate) PLUS a constant of about 20. I cant see to find a way of setting this? Should I have to just create the UILabel programmatically? Can autolayout not help me with this issue?

SleepsOnNewspapers
  • 3,026
  • 3
  • 20
  • 27
  • It's possible both ways: you can calculate the size of the text inside your label and set its frame the right size, or you can use autolayout. It's actually very easy to set autolayout constraints using the Xcode interface builder. – beeef Feb 02 '15 at 23:22
  • I think your answer is in this link @http://stackoverflow.com/questions/8796862/uilabel-auto-size-label-to-fit-text – FamousMaxy Feb 02 '15 at 23:26

1 Answers1

1

In interface builder this will probably be impossible as you've described it. One thing you can do is specify a different value for the label's intrinsic content size. Create a mini UILabel subclass:

@interface Plus20UILabel : UILabel
@end

@implementation Plus20UILabel

-(CGSize)intrinsicContentSize{
    CGSize contentSize = [super intrinsicContentSize];
    return CGSizeMake(contentSize.width + 20, contentSize.height);
} 

@end

Then specify the label is a Plus20UILabel in IB. This will feed the new value into the AutoLayout engine and adjust the label and everything accordingly.

Mike Sand
  • 2,650
  • 12
  • 22