在我的Xamarin.iOS应用程序中,我有一个宽度为300且包含很长字符串的UILabel。我需要调整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.
答案 0 :(得分:2)
您可以使用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;