[英]Dynamic width of collection view cell - Xamarin iOS
我想根據其內容使單元格寬度動態。 我試過通過計算標簽寬度,然后將其分配給單元格框架,但由於標簽未返回實際寬度,因此無法實現。
public void UpdateCell(string text)
{
label.Text = text;
this.ContentView.Frame = new CoreGraphics.CGRect(0, 0, label.Frame.Width, this.ContentView.Frame.Height);
}
您可以使用NSString的GetSizeUsingAttributes
計算字符串的CGRect
,然后可以使用該CGRect
檢索寬度:
using (var labelString = new NSString(label.Text))
{
var size = labelString.GetSizeUsingAttributes(new UIStringAttributes() { Font = label.Font });
ContentView.Frame = new CGRect(0, 0, size.Width, ContentView.Frame.Height);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.