c# – 在Xamarin.iOS中使用填充的UILabel?

我正在尝试在我的Xamarin.iOS应用程序中创建一个带填充的UILabel.本机Objective-C应用程序中最流行的解决方案是覆盖drawTextInRect:
- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0,5,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函数UIEdgeInsetsInsetRect的C#等价物是名为InsetRect的UIEdgeInsets的实例方法,它与您的RectangleF计算(这可能是您的问题)不同.

要使用它,您可以:

public override void DrawText(RectangleF rect)
{
    base.DrawText (Insets.InsetRect (rect));
}

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么