3

If I have a UILabel of a certain size, with line break mode set to WordWrap and numberOfLines = 0 (so it grows depending on the amount of text), is there a way to determine what characters are on each line of the label?

So if i have the text "All People Seem To Need Data Processing" as text on the label, and assuming it displays like this on screen

All People
Seem To
Need Data
Processin
g

is there a way to know the text on each line? or the width of each line?
Line 1 : All People
Line 2 : Seem To
Line 3 : Need Data
Line 4 : Processin
Line 5 : g

GianPac
  • 462
  • 1
  • 7
  • 18
  • 1
    See My answer here - [How to get text from nth line of UILabel?](http://stackoverflow.com/questions/4421267/how-to-get-text-from-nth-line-of-uilabel/14413484#14413484) – TheTiger Jan 19 '13 at 10:46

2 Answers2

0

I have given an answer regarding same query How to get text from nth line of UILabel?.
There is a method, Just call it. It will return an array of each line. Happy Coding :)

Community
  • 1
  • 1
TheTiger
  • 13,264
  • 3
  • 57
  • 82
0

How about using NSString's sizeWithFont? Do up to each word until you get a number bigger than the label's width. Then chop those words and do again for the next line.

CBGraham
  • 1,368
  • 1
  • 13
  • 14
  • 1
    You need to account for spaces and the fact that each glyph has a different width. – Moshe Jan 31 '11 at 18:49
  • to CGGraham : When word wrapping, some characters of the word get chopped like "processing" on the example – GianPac Jan 31 '11 at 19:03
  • Any references as to how UILineBreakModeWordWrap works? I know if it finds certain characters like space, `?`, `-`, newline, it wraps to the next line. But how about when we have a long word? – GianPac Jan 31 '11 at 19:08
  • Moshe - sizeWithFont does that (Why does enter post instead of newline in here? Lame.) /n GianPac - If a word is longer than the width, go letter by letter. And break your list of words on a regex of whitespace plus hyphen. – CBGraham Feb 01 '11 at 17:07