嵌入NSTextAttachment时,高UILabel中缺少行 [英] Lines missing from tall UILabel when embedding NSTextAttachment

查看:427
本文介绍了嵌入NSTextAttachment时,高UILabel中缺少行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用转义的换行符创建多行 NSAttributedString @\ n)。使用iOS 7,我现在可以在归属字符串中嵌入 UIImage (通过 NSTextAttachment )。

I can create a multi-line NSAttributedString by using escaped new-line characters (@"\n"). With iOS 7, I can now embed a UIImage inside attributed strings (via NSTextAttachment).

我注意到每当我将一个 UILabel attributedText 设置为多个-line属性字符串包含嵌入的图像,实际显示的行数与标签的高度成反比。例如,当标签的高度为80时,会出现两条线;当高度大约为100时,只出现第二条线;当高度约为130时,没有任何内容出现。

I have noticed that whenever I set the attributedText of a UILabel to a multi-line attributed string with an embedded image, the number of lines actually displayed is inversely proportional to the height of the label. For example, when the height of the label is 80, two lines appear; when the height is around 100, only the second line appears; when the height is about 130, nothing appears.

尝试在 UITableViewCell内并排放置多个UILabel时出现此问题并让标签与单元格高度一起增长(垂直)。

This problem occurred while trying to position multiple UILabels side-by-side inside a UITableViewCell and having the labels grow (vertically) with the cell height.

任何人都可以解释为什么会这样吗?有没有人知道不涉及使UILabel变小的变通方法吗?

Can anyone explain why this is happening? Does anyone know workarounds that don't involve making the UILabel smaller?

示例代码:

@implementation SOViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc] init];
    [text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 1\n"]];
    [text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 2"]];

    UIImage *image = [UIImage imageNamed:@"17x10"]; //some PNG image (17px by 10px)

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);

    NSMutableAttributedString *text2 = [[NSMutableAttributedString alloc] init];
    [text2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 1\n"]];
    [text2 appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
    [text2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 2"]];

    CGFloat margin = 20;

    //shows both lines when height == 80
    //shows line 2 when 90 <= height <= 120
    //shows nothing when height == 130
    CGFloat height = ???;
    CGFloat width = 200;

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(margin, margin, width, height)];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(margin, margin + height, width, height)];
    [self.view addSubview:label1];
    [self.view addSubview:label2];
    label1.backgroundColor = [UIColor orangeColor];
    label2.backgroundColor = [UIColor blueColor];
    label2.textColor = [UIColor whiteColor];
    label1.numberOfLines = 0;
    label2.numberOfLines = 0;
    label1.attributedText = text1;
    label2.attributedText = text2;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(margin + width, margin + height, image.size.width, image.size.height);
    [self.view addSubview:imageView];
}

@end

...把​​它放进去单一视图应用程序的默认视图控制器。 (您可以选择自己的图像。)

... Put this in the default view controller of a "Single View Application". (You can pick your own image.)

推荐答案

它与NSTextAttachment无关。就像到目前为止发布的iOS 7中一样,UILabel并不擅长绘制属性字符串。带有一些下划线和居中段落样式的简单属性字符串将在UILabel中显示空白或部分空白;相同的属性字符串在UITextView中绘制正常。

It really has nothing to do with the NSTextAttachment. It's that in iOS 7 as released so far, UILabel is not very good at drawing attributed strings. A simple attributed string with some underlining and a centered paragraph style will show up blank, or partially blank, in a UILabel; the same attributed string draws fine in a UITextView.

因此,现在的一个解决方案是:改用UITextView。这实际上是一个非常好的解决方案,因为在iOS 7中,UITextView只是Text Kit堆栈的包装器。因此它以直接的方式绘制属性字符串。它在以前的iOS版本中与Web Kit的底层关系并没有受到任何阻碍。

So, one solution for now is: Use UITextView instead. This is actually a pretty good solution because, in iOS 7, UITextView is just a wrapper around the Text Kit stack. So it is drawing the attributed string in a straightforward way. It is not hampered by the under-the-hood relationship to Web Kit that it had in previous iOS versions.

另一方面,我也找到了一个解决方法这个UILabel的bug;你必须弄乱标签和字符串的行数,以便将文本卡在标签的顶部:请在此处查看我的答案 - https://stackoverflow.com/a/19409962/341994

On the other hand, I have also found a workaround for this UILabel bug; you have to fiddle with the number of lines of the label and the string in such a way as to jam the text up to the top of the label: see my answer here - https://stackoverflow.com/a/19409962/341994

或者你可以等待Apple修复bug并保持手指交叉。编辑:在iOS 7.1中,似乎修复了错误 并且不需要解决方法。

Or you could just wait for Apple to fix the bug and keep your fingers crossed. In iOS 7.1, it appears that the bug will be fixed and no workaround will be needed.

这篇关于嵌入NSTextAttachment时,高UILabel中缺少行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆