我正在尝试在我的Xamarin.iOS应用程序中创建带有填充的UILabel
。在本机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
。
有任何建议吗?
UILabel
没有调整大小以适应插图。不过这可能是一个单独的问题。 - Josh EarlUILabel
不知道插入(并且不会改变它的行为)。 - poupou