0

I have a text view,a 'save' button and a table view in my app.If I want my text in text view to be added to table view I press 'save' button. This is how it works.
Now here come two problems:

  • If text view contains only '\n' I want my button to be disabled
  • My cells in table view are of dynamic height, which means that I calculate number of lines for the label in a table view's cell

So, the first part of the question is: Do I need to parse for all characters in text view and figure out whether they match '\n' and if there are only '\n' symbols in the text view?Are there any ways to do it easier with text view?

The second part: In which way can I calculate row's height when I told that the text view has some '\n' symbols?

Tommy
  • 99,986
  • 12
  • 185
  • 204
efimovdk
  • 368
  • 4
  • 16

1 Answers1

0

To answer your first question, see this SO answer: Check if an NSString is just made out of spaces

You can use the answer that talks about whitespaceAndNewlineCharacterSet, since this would be relevant to what your trying to do.

As for the second part of your question, to calculate the size of a text label given the text in it, there is an easy way to do this:

 CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:YOUR_FONT_SIZE_HERE] constrainedToSize:CGSizeMake(MAX_WIDTH, MAX_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];

Here, "text" is the string you are trying to judge the size of. From this "textSize" you can do

textSize.height

to get the height of this text, and then add whatever padding you may need above and below it to get the final height of the label / row. All of this can probably be done in the method called:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

Which returns a single float, the height of your row.

Community
  • 1
  • 1
BHendricks
  • 4,423
  • 6
  • 32
  • 59