簡體   English   中英

集合視圖單元格的動態寬度-Xamarin iOS

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM