gpt4 book ai didi

iphone - UILabel:仅对文本的一部分应用换行模式

转载 作者:行者123 更新时间:2023-12-01 16:46:54 27 4
gpt4 key购买 nike

考虑长字符串,例如“位于美丽的巴尔的摩市中心”。

我的标签太小,无法显示此文本,目前显示如下:

我希望location子字符串在中心被截断而不截断“Located in”子字符串,如下所示:

位于美丽的…ltimore City

UILabel class reference指示这应该可行:

如果要将换行模式仅应用于文本的一部分,请使用所需的样式信息创建一个新的属性字符串,并将其与标签关联。如果您不使用样式文本,则此属性将应用于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";
}

有没有人得到这个工作?我的实现中缺少什么?

最佳答案

为什么不使用两个UILabel?第一个包含文本“位于”,然后第二个包含您想要的其他任何文本。然后,您可以将NSLineBreakByTruncatingMiddle属性仅应用于第二个标签。
您只需要将它们彼此并排放置,就可以得出幻觉是相同的UILabel

希望这可以帮助!

关于iphone - UILabel:仅对文本的一部分应用换行模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19232006/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com