iPhone如何识别UILabels是否会显示截断的文字?
当UILineBreakModeTailTruncation
启动时,我需要在我的UILabel
上显示更多的button。 即每当“ ...
”出现,我需要显示我的更多的button,一些行动。
我在做什么
float textWidth = [myString sizeWithFont:myLabel.font].width; if (textWidth > myLabel.frame.size.width) { [moreButton setHidden:FALSE]; } else { [moreButton setHidden:TRUE]; }
但是我的问题是,当标签的行数被设置为2时,只要标签的第一行被渲染,就会显示更多的button。
所以我尝试了
if (textWidth > 2*myLabel.frame.size.width) { [moreButton setHidden:FALSE]; } else { [moreButton setHidden:TRUE]; }
这在大多数情况下工作。 但在某些情况下,文本宽度与2 * labelsWidth相同,则会显示更多button。 有没有直接的方法呢?
尝试constrainedToSize变体:
CGSize maxSize = CGSizeMake (myLabel.frame.size.width, 9999); // a really tall frame // this will give you the actual size of your string CGSize actualSize = [myString sizeWithFont:myLabel.font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; if (actualSize.height > myLabel.frame.size.height) { // show your more button }
与string的高度相比,宽度是一样的:
CGSize maximumSize = CGSizeMake(myLabel.frame.size.width, 500); //provide fixed label's width as we will have have fit text in that width. I have used approximately. height can be anything more view' height so take like that CGSize strSize = [str sizeWithFont:[UIFont systemFontSize] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeCharacterWrap]; if(strSize.height > 40) //Calulate height of two lines... { [moreButton setHidden:NO]; //more than 2 lines } else { [moreButton setHidden:YES];//less than 2 lines } }
你可以使用这个函数来获取string的大小:
CGSize size=[myLabel.text sizeWithFont:[UIFont systemFontOfSize:h] constrainedToSize:CGSizeMake(maxWidth, maxHeight) lineBreakMode:UILineBreakModeTailTruncation];
你还应该检查其他sizeWithFont:
函数。