Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consistently draw NSAttributedString in UITextView in UITableViewCell

I am having trouble getting consistent results from UITextViews in a UITableViewCell using NSAttributedStrings.

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    headerText = [[UITextView alloc]initWithFrame:CGRectZero];
    [headerText setUserInteractionEnabled:NO];
    headerText.tag = HEADER_TEXT;
    [cell.contentView addSubview:headerText]; 
} else {
    headerText = (UITextView*)[cell.contentView viewWithTag:HEADER_TEXT];
}

//...setting up attributed strings


[headerText setAttributedText:headerString];

CGSize headerSize = [headerText sizeThatFits:CGSizeMake(246, CGFLOAT_MAX)];

headerText.frame = CGRectMake(45, 8, headerSize.width, headerSize.height);

Results:

Before Scrolling No contentinset

As you can see, the first two appear to draw the text in a way that I would expect/want. In the last two the UITextView sizeThatFits method returns a much larger size then is required to draw the text and the text becomes centered in the frame rather then tight to the top of the frame. This is an issue because I want to be able to layout other views based on the uitextview frame height.

After Scrolling out of frame and back in: enter image description here

Now it gets even stranger, when the cells are reused, and the attributed string is set again. the uitextview draws the text in an inconsistent way.

Even Setting the contentInsets to

headerText.contentInset = UIEdgeInsetsMake(-8, -8, -8, -8);

Does not provide any sort of consistent results:

enter image description here

And After Scrolling with contentinset set: enter image description here

Are there other attributes on UITextView that would allow me to get the behavior that I need?

like image 615
Piotr Tomasik Avatar asked Mar 02 '13 00:03

Piotr Tomasik


1 Answers

When setting the attributed string of a UITextView that previously had a different attributed string, you must always set all of the UITextView's string-related properties to nil first, e.g.:

self.tv.text = nil;
self.tv.font = nil;
self.tv.textColor = nil;
self.tv.textAlignment = NSTextAlignmentLeft;
self.tv.attributedText = s2;

Otherwise, as you have discovered, old features of the previous attributed string still hang around and affect the new attributed string.

In general, though, I have to say I don't see why you're using UITextView at all. If you don't need the user to be able to edit these attributed strings, use UILabel or else just draw the attributed string directly for the most accurate possible rendering. NSAttributedString gives you all the power you need to measure the size and draw within that size.

like image 52
matt Avatar answered Sep 28 '22 07:09

matt