在我的Xamarin.iOS应用程序中,我有一个UILabel,其宽度为300,并且包含一个非常长的字符串。我需要调整UILabel
框架的大小,以便使其高度适合整个字符串,同时保持宽度为300。
UILabel myLabel;
myLabel.Text = "VERY LONG STRING!!!!!!!!111.....youGetThePoint"
myLabel.Font = UIFont.SystemFontOfSize(20, UIFontWeight.Bold);
// now I want to make myLabel's width 300 and the height to be the appropriate amount to see the whole text.
最佳答案
您可以使用 UILabel.sizeThatFits(CGSize size)
:
UILabel myLabel = ...;
myLabel.Text = "VERY LONG STRING!!!!!!!!111.....youGetThePoint"
myLabel.Font = UIFont.SystemFontOfSize(20, UIFontWeight.Bold);
myLabel.PreferredMaxLayoutWidth = 300
myLabel.Lines = 0
var maxSize = new CGSize((nfloat)300, nfloat.MaxValue);
var labelSize = myLabel.SizeThatFits(maxSize);
labelSize
应该为您提供标签显示的非常长的文本的大小。myLabel.Frame.Height = labelSize.Height;
关于ios - 获取高度以适合UILabel中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243167/