Xamarin.iOS 中带有填充的 UILabel?

2024-03-09

我正在尝试创建一个UILabel在我的 Xamarin.iOS 应用程序中使用填充。原生 Objective-C 应用程序中最流行的解决方案是重写drawTextInRect:

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 5};
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

虽然看起来很简单,但我不太清楚如何将其转换为 C#。这是我最好的尝试:

internal class PaddedLabel : UILabel
{
    public UIEdgeInsets Insets { get; set; }

    public override void DrawText(RectangleF rect)
    {
        var padded = new RectangleF(rect.X + Insets.Left, rect.Y, rext.Width + Insets.Left + Insets.Right, rect.Height);

        base.DrawText(padded);
    }
}

这似乎确实移动了标签的文本,但它并没有调整标签的大小。

我认为主要问题是我找不到 Xamarin 等效项UIEdgeInsetsInsetRect.

有什么建议么?


ObjC 函数的 C# 等效项UIEdgeInsetsInsetRect是一个实例方法UIEdgeInsets named InsetRect它与你的不一样RectangleF计算(即likely你的问题)。

要使用它,您可以执行以下操作:

public override void DrawText(RectangleF rect)
{
    base.DrawText (Insets.InsetRect (rect));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin.iOS 中带有填充的 UILabel? 的相关文章

随机推荐