UILabel:将换行符模式仅应用于文本的一部分 [英] UILabel: applying line break mode to only a portion of the text

查看:154
本文介绍了UILabel:将换行符模式仅应用于文本的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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





我希望将位置子字符串中心截断,而不截断位于子字符串,如下所示:


位于美丽的... ltimore City


UILabel类别参考 a>表示这应该是可能的:


如果要仅对文本的一部分应用换行模式,将带有所需样式信息的属性字符串与标签相关联。如果您没有使用样式文本,此属性将应用于text属性中的整个文本字符串。


只有一个UILabel,我试图遵循以下代码的这些说明:

   - (void)viewDidLoad 
{
[super viewDidLoad];

NSString * firstPart = @位于;
NSString * secondPart = @美丽的巴尔的摩市中心;
NSString * joined = [NSString stringWithFormat:@%@%@,firstPart,secondPart];

NSMutableAttributedString * joinedAttributed = [[NSMutableAttributedString alloc] initWithString:joined];

NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingMiddle;

NSRange detailRange = [join rangeOfString:secondPart];

[joinedAttributed addAttribute:NSParagraphStyleAttributeName value:style range:detailRange];

self.label.attributedText = joinedAttributed;
}

我的标签仍然是相同的, p>

调试器中的最终结果如下所示:

  )po joinedAttributed 
位于{
}美丽的巴尔的摩市中心{
NSParagraphStyle =Alignment 4,LineSpacing 0,ParagraphSpacing 0,ParagraphSpacingBefore 0,HeadIndent 0,TailIndent 0,FirstLineHeadIndent 0,LineHeight 0 / 0,LineHeightMultiple 0,LineBreakMode 5,Tabs(\\\
28L,\\\
56L,\\\
84L,\\\
112L,\\\
140L,\\\
168L,\\\
196L,\\\
224L ,\\\
252L,\ n 280L,\ n 308L,\ n 336L \\\
),DefaultTabInterval 0,Blocks(null),Lists(null),BaseWritingDirection -1,HyphenationFactor 0,TighteningFactor 0,HeaderLevel 0 ;
}

有人有这个工作吗?

为什么不使用两个 UILabel s?第一个包含文本位于,然后第二个包含任何您想要的其他文本。然后,您可以将 NSLineBreakByTruncatingMiddle 属性应用于第二个标签。
你只需要把它们放在一起,给出一个错觉: UILabel



希望这有助于!


Consider a long string, such as "Located in beautiful downtown Baltimore City".

My label is too small for this text, and currently displays it like this:

I would like the location substring to be center truncated without truncating the "Located in" substring, like this:

Located in beautiful…ltimore City

The UILabel class reference indicates this should be possible:

If you want to apply the line break mode to only a portion of the text, create a new attributed string with the desired style information and associate it with the label. If you are not using styled text, this property applies to the entire text string in the text property.

In a sample project, with just one UILabel, I attempt to follow these instructions with the following code:

- (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;
}

My label still appears the same, with the truncation at the end.

Here's what the final result looks like in the debugger:

(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";
}

Has anyone gotten this to work? What's missing from my implementation?

解决方案

Why don't you use two UILabels? The first containing the text "Located in " and then the second one containing whatever other text you want. Then you can apply the NSLineBreakByTruncatingMiddle property to the second label only. You just have to position them next to each other to give the illusion that it's the same UILabel.

Hope this helps!

这篇关于UILabel:将换行符模式仅应用于文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆