简体   繁体   中英

UITextView number of lines when not showing full text (ios7)

I have a UITextView that is not showing all the text in it so you need to scroll to read more. i would like to get the number of lines in the UITextView . I tried to find how but all i could find is only if you show all the text to get the number of lines.

autoLayout is on.

my code look like this:

CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.delegate = self;
[self.view addSubview:textView];
[self contentSizeRectForTextView:textView];
textView.text=@"very long text";

i have tried:

UIFont *font = [UIFont boldSystemFontOfSize:11.0];
CGSize size = [textView.text sizeWithFont:font
                        constrainedToSize:textView.frame.size
                            lineBreakMode:NSLineBreakByWordWrapping];
float numberOfLines = size.height / font.lineHeight;
NSLog(@"%f", numberOfLines);

but it gives only the number of lines showed as well as:

float rows = round( (textView.contentSize.height - textView.textContainerInset.top - textView.textContainerInset.bottom) / textView.font.lineHeight );

Thanks in advance

try this!

int numLines = textview.contentSize.height / textview.font.lineHeight;

re-adjust frame:

CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;

textview.scrollEnabled = NO;

You can use UIScrollView and add UITextView as subview. In this case scroll will change size according to text inside UITextView

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