How to align text in a UITextView vertically?

I have a custom cell with UITextView. The length of the text that I upload to this UITextViewone changes from very short to very long. Therefore, please do not say "use a label instead." I want to keep my cells at the same height, but use vertical scrolling UITextViewif the text is too long.

I tried the method here , but since I use custom cell initialization in my cellForRowAtIndexPathevent, the observer did not work. I tried the same method in my cell class in initWithStyle, didn't work either.

What else do you offer? Or should I work with this function differently? Any help is appreciated.

Thanks in advance.

+3
source share
3 answers

In your TableView dataSource -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];

    static NSString *CellIdentifier = @"myCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } else { // if there is already a subview, remove it
        while ([[cell.contentView subviews] count] > 0) {
            UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
            [labelToClear removeFromSuperview];
        }
    }


    UITextView *myTextView = // initialise your textView here, including setting its contentOffset

    [cell.contentView addSubview:myTextView];

    return cell;

}
+1
source

Use it -

CGSize textSize = [myText sizeWithFont:whateverFont constrainedToSize:myTextViewBox.frame.size lineBreakMode:UILineBreakModeWordWrap];

... then compute the contentOffset and set it when you add the UITextView to the table.

+2
source

Here is the entity I did to show how to vertically align a UITextView

+1
source

All Articles