ios - 没有自动换行的多行 UILabel?

标签 ios cocoa-touch uilabel word-wrap

是否可以让一个由多个 \n 分隔的行组成的 UILabel 的行的宽度 > 标签宽度被截断而不换行?

假设我有一些像下面这样的文本:

  1. 这是一个非常长的第一行文本,水平放置太长
  2. 短线
  3. 另一条短线

我希望它像这样出现在我的 UILabel 中:

1. This is a really long first line of text...
2. Short line
3. Another short line

但是发生的事情是我得到这个:

1. This is a really long first line of text  
that is too long to fit horizontally
2. Short line...

第三行被截断了。我已将行数设置为 3,但它仍在换行第一行。我在标签上将换行符属性设置为什么似乎无关紧要——它总是换行第一行。有什么办法可以防止完全缠绕在标签上吗?

最佳答案

我认为这对于您可以应用于标签的任何设置都是不可能的。一种方法是将字符串分成单独的行,截断任何需要它的行,使其(加上省略号)适合一行,然后将字符串放回原处并换行。像这样的东西应该适用于任意数量的行,

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (nonatomic) CGFloat ellipsisWidth;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *text = @"This is a really long first line of text that is too long to fit horizontally\nShort line\nAnother short line";
    NSString *ellipsis = @"...";
    self.ellipsisWidth = [ellipsis sizeWithAttributes:@{NSFontAttributeName:self.label.font}].width;

    __block NSMutableString *truncatedString = [@"" mutableCopy];
    [text enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
        [truncatedString appendFormat:@"%@\n", [self oneLineOfString:line withFont:self.label.font]];
    }];
    NSString *finalString = [truncatedString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    self.label.numberOfLines = 0;
    self.label.text = finalString;
}

-(NSString *)oneLineOfString:(NSString *) aLine withFont:(UIFont *) font {
    __block NSString *singleLine = nil;
    __block NSString *lastFragment;

    [aLine enumerateSubstringsInRange:NSMakeRange(0, aLine.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        NSString *textFragment = [aLine substringToIndex:(substringRange.location + substringRange.length)];
        CGRect textRect = [textFragment boundingRectWithSize:CGSizeMake(CGFLOAT_MAX ,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
        if (textRect.size.width >= self.label.bounds.size.width - self.ellipsisWidth) {
            singleLine = [lastFragment stringByAppendingString:@"..."];
            *stop = YES;
        }
        lastFragment = textFragment;
    }];
    if (!singleLine) singleLine = aLine; // it doesn't need to be truncated, so return the passed in line
    return singleLine;
}

如果你想按字符截断而不是按单词截断,你可以将 NSStringEnumerationByComposedCharacterSequences 而不是 NSStringEnumerationByWords 传递给 options 参数 enumerateSubstringsInRange:options:usingBlock:.

当然,你可以用简单的方法来做;将 3 个标签堆叠在一起,并给每个标签一行你的文字 :)

关于ios - 没有自动换行的多行 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26394249/

相关文章:

Objective-C 将 UILabel 添加到中心

ios - 从 viewController 访问或引用 UIImageView

objective-c - 简单的 bool 切换不起作用?

ios - viewForZoomingInScrollView 工作但有额外的空白空间

objective-c - 自动布局不适用于多行 UILabel

ios - CATextLayer 快速更改文本

iOS 10 : How to repeat local notification once fired on particular date?

ios - 如何为 firebase 数据编写完成处理程序?

iphone - (iPhone) selectedRange 用于不可编辑的 UITextView (或其他点击处理方法?)

ios - 如何通过搜索栏创建到另一个 View Controller 的转场?