我有一个带有图像的属性字符串(NSTextAttachment
)。这没问题,但是我遇到了一个似乎无法解决的截断问题。
在示例中,假设字符串##
是图像。所以我的字符串看起来像Hello world! ##
. 在段落样式上设置尾部截断。
现在,如果空间受到限制,文本将被省略号截断(这是我想要的)。但不幸的是,图像也被截断了。
所以结果是这样的:
Hello w...
但我希望它看起来像:
Hello...##
也就是说,我希望图像附件不会被截断,它应该始终可见。
附件的原因是我希望图像始终位于字符串的末尾,因此当文本较短时,图像位于末尾,而当文本换行为多行时,我也希望图像位于结尾。尝试手动将图像“放在外面”是行不通的,因为文本不会被正确截断。
那么,有没有办法告诉NSAttributedString
不要截断图像?
生成属性字符串的示例代码:
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];
编辑:这其中的一个重要部分(在上面的描述中有点丢失)是这个解决方案需要适用于多行文本。