I have a line with an attached image ( NSTextAttachment). This works well, but I have a truncation problem that I cannot solve.
In the example, suppose the string ##is an image. So my line looks something like this Hello world! ##. The truncation of the tail is set in the style of the paragraph.
Now, if space is limited, the text is truncated using ellipsis (this is what I want). But, unfortunately, the image is also truncated.
So, the result looks something like this:
Hello w...
but I would like it to look like this:
Hello...
That is, I want the image attachment not to be truncated, it should always be visible.
The reason for the attachment is that I want the image to always be at the end of the line, so when the text is short, the image is at the end and when the text wraps over several lines, I also want the image to be at the end. Trying to manually place the image “outside” will not work, as the text will not be truncated correctly.
So, is there any way to say NSAttributedStringnot to truncate the image?
Example code that creates an assigned string:
NSString *title;
NSMutableAttributedString *attributedString;
NSMutableParagraphStyle *paragraph;
NSDictionary *attributes;
NSTextAttachment *attachment;
paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraph.hyphenationFactor = 1.0;
paragraph.lineBreakMode = NSLineBreakByTruncatingTail;
attributes = @{
NSForegroundColorAttributeName : [self titleTextColor],
NSParagraphStyleAttributeName : paragraph,
};
title = @"Hello world!";
attributedString = [[NSMutableAttributedString alloc] initWithString:title
attributes:attributes];
attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"myImage"];
[attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[attachment release];
self.titleLabel.attributedText = attributedString;
[attributedString release];
[paragraph release];
Edit: The crucial part of this (which gets a little lost in the description above) is that this solution should work for multi-line texts.