Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView - Truncate text on iOS 6 showing ellipsis (using Autolayout)

I have a UITextView that displays text longer than what can be fit into the textview's frame. I want the text to be truncated by showing ellipsis on iOS 6.

On iOS 7 I am able to achieve this by using the following code

textView.textContainer.maximumNumberOfLines = 0;
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;

On iOS 6 I am able to truncate the text but it doesn't display the ellipsis.

Problem is on iOS6 the property textContainer is not available

Pls Note:

  • I am using AutoLayout and hence I wouldn't know the frame size when the text is being set. The frame size gets set much later.

Question

On iOS 6 how would I be able to show the ellipsis ?

like image 621
user1046037 Avatar asked Dec 04 '25 01:12

user1046037


1 Answers

Thanks all, @matt helped me find my solution.

I suppose the best fit for my problem is using a UILabel instead.

The only problem I was facing with UILabel and Autolayout was that when the text is just one line, the text was center aligned vertically and it looked odd.

Solution (Autolayout and UILabel)

  1. Use UILabel
  2. Set the preferredMaxLayoutWidth to desired value. This is what I was really missing
  3. Set the height constraint to be less than or equal to desired value
  4. Use sizeToFit after setting the text and after orientation change

So if the height returned by sizeToFit is a smaller height than the max height, then that size would be taken otherwise, max height set in step 3 would be taken

This works on both iOS 6 and iOS 7

like image 132
user1046037 Avatar answered Dec 06 '25 19:12

user1046037



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!