ios - 获取高度以适合UILabel中的字符串

标签 ios xamarin.ios

在我的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/

相关文章:

ios - 由于 libWeANDSFLibrary.a(ASIReachability.o) 而导致的重复符号链接(symbolic link)器错误

xamarin.ios - 如何让 Fody/PropertyChanged 在 Xamarin Studio 的 iOS 项目中工作?

ios - 子类中的委托(delegate)声明与父类中的委托(delegate)不兼容

ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"

iOS 如何使用 ASyncSocket 进行 SSL 握手?

iphone - 我将如何着手测试为 JSON Web 服务下载数据需要多长时间。

ios - 如何将 MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure 设置为 false?

c# - Xamarin iOS UITableView 在初始设置时调用所有单元格的委托(delegate)方法,而不仅仅是可见单元格

ios - C# 规范是否允许从 ulong 转换为 int? Monotouch 给出一个 NumberOverflow

c# - SQLite 未在 MonoDroid 和 MonoTouch 上正确存储 GUID 值