ios 截断 UILabel 中的部分文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20417767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Truncate part of text in UILabel
提问by user2990765
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?
有什么办法可以实现这一目标吗?
回答by Srinivasan N
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
回答by Melanie Journe
For everyone looking for more recent solution, swift 3 :
对于寻找更新解决方案的每个人,swift 3 :
yourLabel.lineBreakMode = .byTruncatingMiddle;
回答by ricks
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
回答by Krishna Kumar
Try this:
尝试这个:
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
UILineBreakModeMiddleTruncation
is deprecated from iOS 6.0.
UILineBreakModeMiddleTruncation
已从 iOS 6.0 弃用。
回答by mak
You can start with finding the length of characters that can be placed in a line, say 'n' characters. 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?. Next, find the length of the string. If it exceeds n, then extract the last two characters. Ex
您可以从查找可以放在一行中的字符长度开始,比如“n”个字符。您可以借助此链接来确定 'n'如何知道 NSString 是否适合 UILabel 以及适合的最后一个字符串的索引?. 接下来,找到字符串的长度。如果超过 n,则提取最后两个字符。前任
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];
回答by iSrinivasan27
回答by Waseem05
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 索引和“.....”中获得的所需字符放入 substringFromIndex 中的字符串中。
hope this helps
希望这可以帮助
回答by Jyothi
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 中,您可以随心所欲地设置换行符模式
回答by jeyachandran
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...
截断字符的简单方法......
回答by Balram Tiwari
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