1

I have a custom table cell with a UILabel in. What I want can be done with two ways.

1- Find the height of the Label that contains long text and set the rowHeight to make the whole text visible.

2- Make the UILabel scrollable so the user can move the text up/down with his hand to read the whole string.

Can you please advice me on which one to do an how to do it?

Panos
  • 7,227
  • 13
  • 60
  • 95
  • This is going to happend programatically I assume? Also, how is the label working? Is it bounded in a width? A little more information first.. – Martol1ni May 16 '12 at 19:39
  • Thanks for the quick answer. Yes I create the custom cell programatically. For now I set the row height to 88 and in the cell I have contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 340, 44)]; contentLabel.tag = 2; contentLabel.font = [UIFont systemFontOfSize:15]; contentLabel.textAlignment = UITextAlignmentLeft; contentLabel.lineBreakMode = UILineBreakModeWordWrap; contentLabel.numberOfLines = 0; contentLabel.text = contentText; [cell.contentView addSubview:contentLabel]; It breaks the lines fine, but if the text is even longer it does not show. – Panos May 16 '12 at 19:49
  • Please update your original post so that the code is displayable.. – Martol1ni May 16 '12 at 19:55
  • Why not to use a UITextView?? – Stas May 16 '12 at 20:31

3 Answers3

1

I recommend not using the first option in most cases as depending on the length of the string it could make your table cells huge.

As for the second option: You should be able to add a UIScrollView as a subview into the table view cells. The Scroll View can contain the fully sized text labels and let you scroll through them within a fixed size cell.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • Do we have any example on how to do it? Im new to iphone, sorry for bugging. – Panos May 16 '12 at 19:56
  • You need to learn 2 things to do this: 1) how to use a UIScrollView. (particularly setting the frame and the contentsize). 2) how to create a custom table view cell (with subviews). Then put it all together by adding the scroll view as a subview in your custom cells. There is plenty of code available on this site and others showing you how to carry out these individual tasks, so good luck! – Dima May 16 '12 at 20:06
1

If your text is static and won't change dynamically, you should use the first option. You can cache the result so whenever you refresh the table, you won't have to recalculate the text. You can use sizeWithFont:constrainedToSize:lineBreakMode to calculate the actual text size.

For the row height: you should use tableView:heightForRowAtIndexPath: and not rowHeight, since rowHeight will change the height for ALL rows - not only a single cell.

A far as a scrollable text view inside a table: in my opinion - it looks like a cheap solution in an app and makes it a bit amateur.

Gilad Novik
  • 4,546
  • 4
  • 41
  • 58
1

Do we have any example on how to do it? Im new to iphone, sorry for bugging. – Panos 9 mins ago

UIScrollView *scroller = [[UIScrollView alloc]initWithFrame:cell.frame];
scroller.contentSize = CGSizeMake(280, 100);
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
test.text = @"This would be your label.";
[scroller addSubview:test];
[cell addSubview:scroller];

This will make a UIScrollView and a label in the tablecell 'cell'. This will work, but I do not think this is the best solution, since it's gonna be a scrollview inside a tableview which already provides scrolling. My advice is to adjust the rowheigth.

Martol1ni
  • 4,684
  • 2
  • 29
  • 39