c# - Xamarin.iOS 中带有填充的 UILabel?

标签 c# ios iphone xamarin.ios uilabel

我正在尝试在我的 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# 等效项是名为 InsetRectUIEdgeInsets 实例方法,它与您的 RectangleF 不同 计算(这可能是你的问题)。

要使用它,您可以:

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

关于c# - Xamarin.iOS 中带有填充的 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164506/

相关文章:

ios - 有什么方法可以管理 iPhone 应用程序中的 session 吗?

c# - 如何从弹出消息导航到新页面?我正在使用 Xamarin 社区工具包弹出窗口

c# - 最新 DotNet 技术的最佳在线视频培训网站

ios - 检查一个词后面是否有一个词(NSSTRING)

ios - NSFetchedResultsControllerDelegate 在错误的 indexPath 上动画删除

iphone - Interface Builder 对象库中缺少对象

ios - 在 iOS 7 上获取互联网/网络的状态

ios - 如何在 Objective-C 中表示表格数据

c# - HttpHandler 仅在文件不存在时触发

c# - 列表框选择模式在 WPF 中不起作用