Parth Bhatt
Parth Bhatt

Reputation: 19479

iPhone : How to obtain the number of lines entered in textView without consideration of width

In my iPhone app, I have a textview where in the user will input multi-line data.

Now I need a solution where I can get the number of lines.

I need a solution which does not have consideration of width.

I need only number of lines entered by user in textView.

How can I do so?

Upvotes: 0

Views: 776

Answers (3)

Alex Terente
Alex Terente

Reputation: 12036

By line you mean when user press the return key on the key board? If so count the \n in it. Hope that helps.

NSArray *arrayWithLines = [myString componentsSeparatedByString:@"\n"];
NSLog(@"Number of Lines:%@",[arrayWithLines count]);

Upvotes: 0

Björn
Björn

Reputation: 29411

Since you said "without consideration of width" and not "without consideration of height", this is how you do it:

int numberOfLines = yourUITextView.contentSize.height / yourUITextView.font.lineHeight

The UIFont property lineHeight is new from iOS4.0, for older versions use leading property.

Edit,

this answer applies to text views where there have been automatically inserted line breaks (i.e. the line got to long), if you only want to know the number of manually inserted line breaks, Terentes answer would do it.

To count manually inserted line breaks, this hack should do it:

int numberOfManuallyBreaks = [[yourUITextView componentsSeparatedByString:@"\n"] count];

Upvotes: 2

Aditya
Aditya

Reputation: 4396

Have you tried fetching the number of lines by using the numberOfLines property of TextView.

Upvotes: 0

Related Questions