OGeek|极客世界-中国程序员成长平台

标题: ios - Collection View 单元格的动态宽度 - Xamarin iOS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:46
标题: ios - Collection View 单元格的动态宽度 - 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);
}



Best Answer-推荐答案


您可以使用 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);
}

关于ios - Collection View 单元格的动态宽度 - Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46405637/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4