考虑一个长字符串,例如“位于美丽的巴尔的摩市中心”。
我的标签对于此文本来说太小了,目前显示如下:
, 像这样:
位于美丽的……ltimore 市
UILabel 类参考表明这应该是可能的:
如果您只想将换行模式应用于文本的一部分,请使用所需的样式信息创建一个新的属性字符串并将其与标签相关联。如果您不使用样式文本,则此属性适用于 text 属性中的整个文本字符串。
在一个示例项目中,只有一个 UILabel,我尝试使用以下代码按照这些说明进行操作:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *firstPart = @"Located in";
NSString *secondPart = @"beautiful downtown Baltimore City";
NSString *joined = [NSString stringWithFormat:@"%@ %@", firstPart, secondPart];
NSMutableAttributedString *joinedAttributed = [[NSMutableAttributedString alloc] initWithString:joined];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingMiddle;
NSRange detailRange = [joined rangeOfString:secondPart];
[joinedAttributed addAttribute:NSParagraphStyleAttributeName value:style range:detailRange];
self.label.attributedText = joinedAttributed;
}
我的标签仍然显示相同,最后被截断。
以下是调试器中的最终结果:
(lldb) po joinedAttributed
Located in {
}beautiful downtown Baltimore City{
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 5, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}
有没有人让这个工作?我的实施缺少什么?