UITextView 末尾的省略号

发布于 2024-11-30 06:35:08 字数 319 浏览 3 评论 0原文

如果我有多行不可滚动的 UITextView,其文本长度超过了可见区域的容纳范围,那么文本就会像这样被截断:

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

如何让文本在文本截断位置处显示省略号,像这样

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

其他控件(如标签和按钮)也具有此功能。

If I have multi-line non-scrollable UITextView whose text is longer than can fit in the visible area, then the text just cuts off like so:

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

How would I get the text to show with an ellipsis where the text cut-off is, like so

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

Other controls like labels and buttons have this ability.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月染过的梦 2024-12-07 06:35:09

为什么不使用 UILabelnumberOfLines 设置为适当的值并免费获得该功能?

Why not use a UILabel setting numberOfLines to something appropriate and getting that functionality for free?

若相惜即相离 2024-12-07 06:35:09

UITextView 设计为当字符串大于视图可以显示的内容时滚动。确保您已在代码或 xib 中正确设置锚定和自动调整大小属性。

这是 博客文章

@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

The UITextView is designed to scroll when the string is larger than what the view can show. Make sure that you have set the anchoring and autoresize attributes correctly in code or your xib.

Here is an example from a blog post about 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
影子是时光的心 2024-12-07 06:35:09

有人刚刚向我展示,在 iOS 7 及更高版本上使用 UITextView 做到这一点实际上非常容易:

UITextView *textView = [UITextView new];
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;

Someone just showed me that it's actually really easy to do this with UITextView on iOS 7 and up:

UITextView *textView = [UITextView new];
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文