【问题标题】:Xamarin.Forms - possible to add padding to label using custom renderer? (iOS)Xamarin.Forms - 可以使用自定义渲染器向标签添加填充? (iOS)
【发布时间】:2016-07-10 20:20:20
【问题描述】:

有没有办法使用自定义渲染器向标签添加填充?我知道您可以通过在标签周围添加内容视图并向内容视图添加填充来作弊;但我想保持 UI 更干净,并且不必添加额外的元素。

为了清楚起见,我不想要边距 - 换句话说,如果我向标签添加背景颜色,您应该会看到文本和标签背景之间的填充,如下所示:

【问题讨论】:

  • 我找不到任何使用样式或自定义渲染的东西。我知道你不想让你的 UI 变得复杂,但是一个文本字段与一些背景重叠的自定义控件呢?从本质上讲,这是您要避免的,但您的 UI 中没有杂乱的代码。

标签: label padding xamarin.forms custom-renderer


【解决方案1】:

你有没有尝试过这样的事情:

namespace CustomFinder.iOS.Renderers
{   
    public class DataLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            if (Control == null)
            {
                SetNativeControl(new TagUiLabel());
            }

            base.OnElementChanged(e);
        }
    }

    public class TagUiLabel : UILabel
    {
        private UIEdgeInsets EdgeInsets { get; set; }

        public TagUiLabel()
        {
            EdgeInsets = new UIEdgeInsets(0, 3, 0, 3);
        }
        public override void DrawText(CoreGraphics.CGRect rect)
        {
            base.DrawText(EdgeInsets.InsetRect(rect));
        }
    }
}

我有这个来自here 还没试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2014-08-29
    • 2016-05-31
    • 2014-08-10
    • 2017-01-07
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多