1

I have a long string to show on UIButton. I want to show the first 2 lines and if the text is longer than that - add a More button that will pop up an alertview to show the full text. See the image

enter image description here

What is the best way to do that?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Ali Sufyan
  • 2,038
  • 3
  • 17
  • 27

2 Answers2

0

Add your more button and wire it up the standard way, but set it to hidden. Then determine the length of the string and see if it is bigger than your textview. If so, set the more button to visible. When the button is pressed resize the textview and add more lines.

William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • @AzkaarAli Which part of the answer are you having trouble with? – Stephen Melvin Oct 25 '13 at 14:19
  • What do you mean by length in this context? String length? The CGRect that bounds the text on screen? If the latter, it seems you can't directly compare the size of an NSString on screen with the size of a UITextView because UITextViews border their content. [Source](http://stackoverflow.com/a/5641169/257533) – Stephen Melvin Oct 25 '13 at 14:28
  • @StephenMelvin can you please help me solve this problem. I am still unable to do this. – Ali Sufyan Oct 28 '13 at 09:49
  • @AzkaarAli, What are you having trouble with? – Stephen Melvin Oct 28 '13 at 13:14
0

I'm not around a Mac at the moment so don't take this answer as gospel.

I would think that if you added a UILabel as a subview of a UIButton and setup the label to only show two lines before truncating, you could then detect if the displayed text was different than the actual text you used when you created the label by using NSString's isEqualToString: method. If the strings are different you know the label is truncated and you should show the 'more' button. I found this code on StackOverflow that returns an NSString within an arbitrary NSRect.

Community
  • 1
  • 1
Stephen Melvin
  • 3,696
  • 2
  • 27
  • 40