简体   繁体   English

如何使UILabel在字符边界处截断?

[英]How to make a UILabel truncate at character boundaries?

I have some UILabels next to each other horizontally which will contain names. 我有一些UILabel彼此水平相邻,其中包含名称。

I can't find any way of getting the text to be truncated at a character boundary - I've tried a line break mode with NSLineBreakByCharWrapping but the characters get chopped off in the middle 我找不到任何在字符边界处截断文本的方法-我尝试了使用NSLineBreakByCharWrapping的换行模式,但是字符在中间被切掉了

在此处输入图片说明

I've tried the other line break modes but can't get rid of the partial character (x in this particular example), I don't want part of a character displayed obviously as it looks no good. 我尝试了其他换行模式,但无法摆脱部分字符(在此特定示例中为x),我不希望部分字符明显显示,因为它看起来不好。

Its not feasible to limit the output to a hard coded number of characters because iiiiiiii is a totally different width to wwwwwwww for example. 将输出限制为硬编码的字符数是不可行的,因为iiiiiiii是与wwwwwwww完全不同的宽度。 Also I don't want … appearing within the text because as its narrow then there would be too few characters left if part of the available space is consumed with ... 我也不希望...出现在文本中,因为它的宽度太窄,如果用...占用了部分可用空间,那么字符将太少了。

    contactItem.name.frame = nameFrame;
    contactItem.name.font = [UIFont systemFontOfSize:12];
    contactItem.name.textColor = [UIColor whiteColor];
    contactItem.name.textAlignment = NSTextAlignmentCenter;
    contactItem.name.lineBreakMode = NSLineBreakByCharWrapping;
    [self.scrollView addSubview: contactItem.name];

contactItem.name is the UILabel(s). contactItem.name是UILabel。 nameFrame is getting horizontally incremented for each contactItem. 对于每个contactItem,nameFrame都在水平方向递增。

Maybe calculate text size yourself and react when it's too long? 也许自己计算文本大小并在时间过长时做出反应?

Calculating UILabel Text Size 计算UILabel文本大小

I don't know if this the simplest solution for this problem - but for sure it's the working solution. 我不知道这是否是解决此问题的最简单方法-但可以肯定的是这是可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM