Линии, отсутствующие в высокой UILabel при встраивании NSTextAttachment

Я могу создать многострочноеNSAttributedString с помощью экранированных символов новой строки (@"\n"). С iOS 7 теперь я могу вставлятьUIImage внутри приписанных строк (черезNSTextAttachment).

Я заметил, что всякий раз, когда я устанавливаюattributedText изUILabel для строки с несколькими строками и встроенным изображением, количество отображаемых строкобратно пропорционально высоте этикетки. Например, когда высота метки равна 80, появляются две строки; когда высота около 100, появляется только вторая строка; когда высота около 130, ничего не появляется.

Эта проблема возникла при попытке расположить несколько UILabel рядом друг с другомUITableViewCell и когда метки растут (вертикально) с высотой клетки.

Кто-нибудь может объяснить, почему это происходит? Кто-нибудь знает обходные пути, которые не включают уменьшение UILabel?

Образец кода:

@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

... Поместите это в контроллер представления по умолчанию для "Single View Application". (Вы можете выбрать свое собственное изображение.)

Ответы на вопрос(4)

Ваш ответ на вопрос