1

I have a textview which will be displaying four lines of text. If the text is too long, I need to show "See more" at the end of the fourth line. If text is small, I shouldn't show the "See more". I searched a lot, nothing helping meenter image description here

venky
  • 1,155
  • 1
  • 11
  • 26

3 Answers3

1

In order to do this, you have to use an NSTextContainer. You can do some pretty fancy stuff with this, such as forming your text in a circle, triangle, or whatever UIBezierPath you can think of...

Here's apple's documentation: https://developer.apple.com/reference/uikit/nstextcontainer

You could then make a UIBezierPath shaped like this:enter image description here

In the bottom right corner, you could have your "See more" button.

If the text is too big (see lineBreakMode), then enable the button with

seeMoreButton.enabled = true

If the text fits, use

seeMoreButton.enabled = false

bearacuda13
  • 1,779
  • 3
  • 24
  • 32
0

You'll have to use the full Text Kit stack and lay the text out yourself. Apple explained how to do this for text truncation in a WWDC video a couple of years ago.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

One way you could do this is have a max character count for the label and take the text up to that count and append with the "... See more" text like:

label.text = randomPost.text.substring(0, maxChars) + "... See more"

Note that I'm going off memory on how to substring in Swift, the signature I've provided may be off, just trying to convey the idea.

rigdonmr
  • 2,662
  • 3
  • 26
  • 40
  • How do I know maxChars for different devices? – venky Jul 19 '16 at 18:32
  • [This](http://stackoverflow.com/questions/25193520/determine-the-maximum-number-of-characters-a-uilabel-can-take) will give you maximum number of characters a UILabel can take! – D4ttatraya Jul 19 '16 at 19:13
  • @venky that's a different question. There's different ways to find out what device you're using. – rigdonmr Jul 19 '16 at 20:16