2

I'm developing an iOS app to show the information about my facebook friends in a UITableView and I'm with a problem. I use a custom cell with 2 UILables (Field and description):

enter image description here

In the field label I set the category of the information like education, work, etc. In the description label I set the respective information:

enter image description here

My problem is that the label does not fit the size of the information and cuts off part of it as may be seen above. I'm tired of searching for the solution and I find nothing. I already tried to implement the code that is in this answer (Adjust UILabel height depending on the text) and not working!

Any suggestion? Tks

Community
  • 1
  • 1
Tiago Pereira
  • 572
  • 1
  • 4
  • 19
  • that answer you refer to looks promising, pls post the code you tried. an alternative would be a UITextField. Kind of heavyweight, though. – danh Dec 14 '13 at 20:39

2 Answers2

1

UILabels are good, but why don't you use UITextView? You could use \n in textView, i can see that you have large text in label, it's not really good decision to scale font size depending on your text size. Just add textView to your custom cell, disable user interaction, set editable to NO, and you're done. You also can use this code to change cell height, depending on text size:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Load text in NSString which you want in Cell's textView.
    NSString *cellText;

    cellText = @"Insert your text here";

    //define font for textView...
    UIFont *cellFont = [UIFont fontWithName:@"Georgia" size:19.0];//change your font here

    CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);

    CGSize labelSize_val = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

    return labelSize_val.height + 20;
} 
ignotusverum
  • 3,466
  • 2
  • 28
  • 60
0

The answer that you found at Adjust UILabel height depending on the text is a good start.

Additionally make sure that you set row height with: https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

The other area to be cautious about are the autoresize settings of your UILabel.

Community
  • 1
  • 1
pkuhar
  • 541
  • 5
  • 16