简体   繁体   English

截断UILabel中的部分文本

[英]Truncate part of text in UILabel

My requirement is that I need to display text in label in such a way that if the length of text is too big to accommodate in one line, i need to truncate it at the end in such a way that only the last few characters(usually a number b/w 1-1000 so text length may vary.) are visible and the text before it is truncated with "...". 我的要求是我需要在标签中显示文本,如果文本的长度太大而无法容纳在一行中,我需要在最后以最后几个字符(通常是最后几个字符)截断它一个数字b / w 1-1000所以文本长度可能会有所不同。)是可见的,其前面的文本被截断为“...”。

So the text will look something like "abcdefgijk...10" 所以文字看起来像“abcdefgijk ... 10”

Is there any way I can achieve this? 有什么办法可以实现吗?

UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(50,100, 150, 30)];
contentLabel.text = @"abcdefghijklmnopqrstuvwxyz10";
contentLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;

Add this label to your display. 将此标签添加到您的显示器。 You should get a output something like this 你应该得到这样的输出

abcdefghijklmnopq...10

对于每个寻找更新解决方案的人来说,swift 3:

yourLabel.lineBreakMode = .byTruncatingMiddle;

Swift 4: 斯威夫特4:

Incase someone runs into this issue like i did, 有人像我一样遇到这个问题,

you need to set your label.numberOfLines = 1 你需要设置label.numberOfLines = 1

if you have it set to 0 it will truncate at a space. 如果你将它设置为0,它将在空格处截断。

so your code should look like 所以你的代码应该是这样的

    label.numberOfLines = 1
    label.lineBreakMode = .byTruncatingTail
    label.adjustsFontSizeToFitWidth = false

Try this: 尝试这个:

 label.lineBreakMode = NSLineBreakByTruncatingMiddle;

UILineBreakModeMiddleTruncation is deprecated from iOS 6.0. 从iOS 6.0不推荐使用UILineBreakModeMiddleTruncation

In Storyboard 在故事板中

  1. Select the Label which you want to truncate the characters. 选择要截断字符的标签。

  2. Choose Attributes Inspector. 选择“属性检查器”。

  3. Under Label attributes. 在标签属性下。 You can find Line Break 你可以找到Line Break

在此输入图像描述

Done. 完成。

there are many methods in NSString class use -length and then use any of these NSString类中有很多方法使用-length然后使用其中任何一个

– substringFromIndex:
– substringWithRange:
– substringToIndex:

create a temporary string using NSString stringwithFormat, put your desired charecters you get from substringTo index and "....." then your numbers from string by substringFromIndex. 使用NSString stringwithFormat创建一个临时字符串,将你从substringTo索引得到的所需字符和“.....”从字符串by substringFromIndex得到你想要的字符串。

hope this helps 希望这可以帮助

You can start with finding the length of characters that can be placed in a line, say 'n' characters. 您可以从找到可以放在一行中的字符长度开始,比如'n'字符。 You can take help of this link to determine 'n' How to know if NSString fits in UILabel or not and index of the last string which fits? 您可以借助此链接来确定'n' 如何知道NSString是否适合UILabel以及最后一个适合的字符串的索引? . Next, find the length of the string. 接下来,找到字符串的长度。 If it exceeds n, then extract the last two characters. 如果它超过n,则提取最后两个字符。 Ex 防爆

NSString * fooString = @"a very long string";
NSString * s2 = [fooString substringWithRange:NSMakeRange([fooString length]-3, 2)];
NSString * s1 = [fooString substringWithRange:NSMakeRange(0 , n-5)];
NSString * newString = [NSString stringWithFormat:@"%@...%@",s1,s2];

We have different Line Break modes for UILabel like 我们为UILabel提供了不同的换行模式

Truncate Head, Truncate Middle, Truncate Tail 截断头部,截断中部,截断尾部

In Xib you can set the Line Break mode what ever you want 在Xib中,您可以根据需要设置换行模式

If you using XIB file.. 如果您使用XIB文件..

select --> UILable and select --> Attribute inspector tag and change into Line Breaks-->Truncate tail 选择 - > UILable并选择 - >属性检查器标签并更改为换行符 - >截断尾部

simply way to truncate characters... 简单地截断字符的方法......

Here is how to use it, NSLineBreakByTruncatingMiddle 以下是如何使用它, NSLineBreakByTruncatingMiddle

UILabel *temp = [[UILabel alloc]initWithFrame:CGRectMake(5,75, 100, 50)];
[temp setBackgroundColor:[UIColor lightGrayColor]];
temp.lineBreakMode = NSLineBreakByTruncatingMiddle;
temp.text = @"HelloBoss997";

Output : Hello...s997 输出: Hello...s997

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

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