在 Xamarin.iOS 中带有填充的 UILabel? [英] UILabel with padding in Xamarin.iOS?
问题描述
我正在尝试在我的 Xamarin.iOS 应用程序中创建一个带有填充的 UILabel
.原生 Objective-C 应用程序中最流行的解决方案是覆盖 drawTextInRect
:
I'm trying to create a UILabel
with padding in my Xamarin.iOS app. The most popular solution in native Objective-C apps is overriding drawTextInRect
:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
这看起来很简单,但我无法弄清楚如何将其转换为 C#.这是我最好的尝试:
As simple as this seems, I can't quite figure out how to translate it to C#. Here's my best stab at it:
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);
}
}
这似乎会移动标签的文本,但不会调整标签的大小.
This does seem to move the label's text, but it doesn't resize the label.
我认为主要问题是我找不到 UIEdgeInsetsInsetRect
的 Xamarin 等效项.
I think the main issue is that I can't find the Xamarin equivalent of UIEdgeInsetsInsetRect
.
有什么建议吗?
推荐答案
ObjC 函数 UIEdgeInsetsInsetRect
的 C# 等价物是名为 InsetRect 的
并且它与您的 UIEdgeInsets
实例方法RectangleF
计算不同(这可能是您的问题).
The C# equivalent of the ObjC function UIEdgeInsetsInsetRect
is a instance method of UIEdgeInsets
named InsetRect
and it's not identical to your RectangleF
calculations (which is likely your problem).
要使用它,您可以:
public override void DrawText(RectangleF rect)
{
base.DrawText (Insets.InsetRect (rect));
}
这篇关于在 Xamarin.iOS 中带有填充的 UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!