我正在尝试在我的 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);
}
}
这似乎确实移动了标签的文本,但并没有调整标签的大小。
我认为主要问题是我找不到与 UIEdgeInsetsInsetRect
等效的 Xamarin。
有什么建议吗?
最佳答案
ObjC 函数 UIEdgeInsetsInsetRect
的 C# 等效项是名为 InsetRect
的 UIEdgeInsets
实例方法,它与您的 RectangleF 不同
计算(这可能是你的问题)。
要使用它,您可以:
public override void DrawText(RectangleF rect)
{
base.DrawText (Insets.InsetRect (rect));
}
关于c# - Xamarin.iOS 中带有填充的 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164506/