Ellipsis at the end of a UITextView

If I have a multi-line, non-programmable UITextView whose text is longer than it can fit in the visible area, then the text simply turns off like this:

Congress shall make no law respecting an establishment of religion, or 

How do I get the text to display using the ellipsis where the text is turned off, for example

 Congress shall make no law respecting an establishment of religion, or … 

Other controls, such as labels and buttons, have this ability.

+8
ios objective-c iphone uitextview
source share
3 answers

Why not use the UILabel numberOfLines parameter for something suitable and get this functionality for free?

+6
source share

UITextView designed to scroll when the row is larger than shown in the view. Make sure you set the binding and auto-resolution attributes correctly in your code or your xib.

Here is an example from a post on how to implement your own ellipsis.

 @interface NSString (TruncateToWidth) - (NSString*)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font; @end 

 #import "NSString+TruncateToWidth.h" #define ellipsis @"…" @implementation NSString (TruncateToWidth) - (NSString*)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font { // Create copy that will be the returned result NSMutableString *truncatedString = [[self mutableCopy] autorelease]; // Make sure string is longer than requested width if ([self sizeWithFont:font].width > width) { // Accommodate for ellipsis we'll tack on the end width -= [ellipsis sizeWithFont:font].width; // Get range for last character in string NSRange range = {truncatedString.length - 1, 1}; // Loop, deleting characters until string fits within width while ([truncatedString sizeWithFont:font].width > width) { // Delete character at end [truncatedString deleteCharactersInRange:range]; // Move back another character range.location--; } // Append ellipsis [truncatedString replaceCharactersInRange:range withString:ellipsis]; } return truncatedString; } @end 
+4
source share

Someone just showed me that this is actually very easy to do with a UITextView on iOS 7 and above:

 UITextView *textView = [UITextView new]; textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail; 
+2
source share

All Articles