我正在编程Objective C
。我有一个UITableView
与UILabel
的。如何检测UILabel
中的省略号?如何检测UILabel中的省略号?
回答
首先,我们可以得到,这将在标签被渲染文本的宽度。然后我们可以将该宽度与标签的宽度进行比较。如果该宽度超过,则字符串被截断,否则截断。
UPDATE
如果标签是有行再算上线的数量和核对lablewidth * numofLines
UILabel *lblAppTitle = (UILabel*)[self.view viewWithTag:777];
CGSize stringSize = [lblAppTitle.text sizeWithFont:lblAppTitle.font];
//Count Number of lines
[lblAppTitle sizeToFit];
int numLines = (int)(lblAppTitle.frame.size.height/lblAppTitle.font.leading);
if (stringSize.width > (lblAppTitle.frame.size.width)*numLines)
NSLog(@"truncated string");
else
NSLog(@"did not truncate string");
希望这有助于你。
+1在发布我之前,我没有看到您的答案。这是正确的方法。 – 2013-03-21 06:26:39
+1此答案将起作用。不是我的一个:( – 2013-03-21 06:29:47
@AnoopVaidya这种方法不能正常工作,所有行数大于等于2的标签都被截断,但有两行的标签可能不会被截断 – Sveta 2013-03-21 07:18:57
NSString *str = @"Hello this is the ...";
NSRange range = [str rangeOfString:@"..."];
if (range.location>0 && range.length == 3) {
//Found
}
希望这会对你有帮助。
UILabel *lbl ;
lbl.text = @"Hello..World.";
NSString * charToCount = @".";
NSArray * array = [lbl.text componentsSeparatedByString:charToCount];
if([array count] >= 3)
NSLog(@"Found");
else
NSLog(@"Not Found");
希望它会帮助你...
你是什么意思3点?你想要达到什么目的? – Kunal 2013-03-21 06:02:17
是你想检查一个像a.bd.jdkjd一样的3dots。或abd ... hdh? – 2013-03-21 06:02:41
3个点意味着省略号或三个点在字符串中的任何位置? – 2013-03-21 06:06:05