简体   繁体   English

集合视图单元格的动态宽度-Xamarin iOS

[英]Dynamic width of collection view cell - Xamarin iOS

I want to make Cell width dynamic with respect to its content. 我想根据其内容使单元格宽度动态。 i've tried by calculating label width and then assigning it to cell frame but couldn't achieve this because label is not returning it actual width. 我试过通过计算标签宽度,然后将其分配给单元格框架,但由于标签未返回实际宽度,因此无法实现。

public void UpdateCell(string text)
{
    label.Text =  text;
    this.ContentView.Frame = new CoreGraphics.CGRect(0, 0, label.Frame.Width, this.ContentView.Frame.Height);
}

You can use the NSString's GetSizeUsingAttributes to calculate the CGRect of a string, you can then use that CGRect to retrieve the width: 您可以使用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