简体   繁体   中英

UITextView doesn't scroll sometimes

While typing in a UITextView sometimes it scrolls down to current line ( case a ) but it doesn't the other times( case b ).

There's another problem which is:

The same UITextView sometimes show all the text in it ( case 1 ) but other times it doesn't show the last line of text( case 2 ).

Whenever case 1 happens case a follows. and Whenever case 2 happens case b follows.

This is the hierarchy of the view: 在此处输入图片说明

Size( variable height-fixed width ) of these UITextView s as well as UICollectionViewCell s are calculated using sizeWithFont:constrainedToSize:lineBreakMode:

Limits of height are set from 43 to 120.

if height>43 then enableScrolling is set to YES , otherwise to NO ( Logic X ).

Scrolling is enabled when textViewBeginEditing and Logic X is applied when textViewEnded Editing.

There is no scrolling in case 2 .

Please suggest cause and workarounds.

On iOS7, I think that you could leave the UITextView's scrollEnabled property set to YES in all cases. If it contains less text it will just not scroll. If you set the property while configuring the cell, you might get this kind of weird behavior, because UIKit is reusing those cells and probably the UITextView too.

For making the last line visible, I'm guessing you need to calculate the text view size more accurately. Try using the attributedText to set the text, with all the formatting you need. Then, in order to calculate the size, you can do it like this:

NSAttributedString *text = self.yourCellsTextView.attributedText;
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(self.yourCellsTextView.frame.size.width, FLT_MAX)];
CGFloat finalMessageHeight = size.height;

Hope this helps :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM