iWizard
iWizard

Reputation: 7144

How to calculate number of characters in second line of label?

I've previously asked how to put whole word in second line (word wrap). Now I'm interested in following thing. For example I have this string: "Lorem ipsummmmmm long title" and I've set "setLineBreakMode" for label and everything is ok except one thing. Some words are long and let we suppose that word in my string "ipsummmmmm" can't be in first line su it's in second line. And now I've got problem because I set for whole label to cut strings longer than 27 characters nad problem is that in first line will be only "Lorem" and other three words will be in second line.

My code for substring:

mainArticleTitleLabel.text = [finalMainArticleTitle stringByAppendingString:@"..."];

Is there any way to calculate and solve that because somewhere I got @"..." and somewhere is only cut word?

Upvotes: 1

Views: 669

Answers (1)

BMcGlade
BMcGlade

Reputation: 169

I've got an idea on how to do it, but it's probably not the quickest way to solve it.

What I would suggest, is to separate the original string into an array of words. You then begin rebuilding the string, word by word. After adding a word, you can use the [sizeWithFont: constrainedToSize:] method, giving the label width and a large number, ie 10,000 for the height. Once the height jumps from 1 line to 2, then you know what was in the first line. Continue to add words until you either run out of words, or hit the 3rd line, then you can calculate the character count of line 2.

I hope this solves your problem, or gets you on the right track at least.

Upvotes: 2

Related Questions