jeudi 13 août 2015

UILabel dynamic height

I know a lot of questions have been asked about it but could not figure out. I have a label that needs its number of lines adusting dynamically. It works with automatic preferred max width, but could not make it work when explicit (I want to support ios7).

The label height does not currently increase

Here is my code:

-(void)viewDidLoad
{
  [super viewDidLoad];
  _feedBackLabel.numberOfLines = 0;
  _feedBackLabel.lineBreakMode = NSLineBreakByWordWrapping;
  _feedBackLabel.preferredMaxLayoutWidth = self.view.frame.size.width - 90;
  [_feedBackLabel setContentHuggingPriority:UILayoutPriorityRequired
                                  forAxis:UILayoutConstraintAxisVertical];
  [_feedBackLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}

-(void)setFeedback:(NSString*)response
{
  self.feedBackLabel.text = response;
  self.feedBackLabel.hidden = NO;
  [self.feedBackLabel sizeToFit];
}

I have pinned my label with a leading and a trailing space to its superview with priority required. The content hugging and compression resistance are both 750 in the vertical axis. The label has a height constraint with a priority of 500.

Thanks Guillaume



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire