如何在iOS渲染器中制作标签并调整大小?

时间:2017-11-14 10:33:48

标签: xamarin xamarin.ios xamarin.forms

我在iOS渲染器中有这段代码:

    private class CustomFooterTableViewModelRenderer : TableViewModelRenderer
    {
        public CustomFooterTableViewModelRenderer(TableView model) : base(model)
        {
        }

        public override UIView GetViewForFooter(UITableView tableView, nint section)
        {
                var lbl = new PaddedLabel()
                {

                    // Text = TitleForFooter(tableView, section), // or use some other text here
                    Text = "Select or deselect cards from the list above and they will added or removed from the card deck",
                    TextAlignment = UITextAlignment.Justified,
                    LineBreakMode = UILineBreakMode.WordWrap
                };
                return lbl;
        }

        public sealed class PaddedLabel : UILabel
        {
            private UIEdgeInsets EdgeInsets { get; set; } = new UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 0);

            public override void DrawText(CoreGraphics.CGRect rect)
            {
                var newRect = new CoreGraphics.CGRect(0, 0, rect.Width + 10, rect.Height);
                base.DrawText(EdgeInsets.InsetRect(newRect));
            }
        }

    }

代码有效,但我的标签没有环绕,我猜它没有调整大小也注意我确实尝试添加WordWrap但似乎没有工作。任何人都有任何想法,我如何解决这个问题,并使其自动化。

2 个答案:

答案 0 :(得分:2)

而不是UILabel,尝试使用'UITextView` - 更容易将自动换行与填充/插入集成。

private class CustomFooterTableViewModelRenderer : TableViewModelRenderer
{
    public CustomFooterTableViewModelRenderer(TableView model) : base(model)
    {
    }

    public override UIView GetViewForFooter(UITableView tableView, nint section)
    {
        var txtView = new UITextView
        {
            Editable = false,
            Text = "Select or deselect cards from the list above and they will added or removed from the card deck,",
            TextAlignment = UITextAlignment.Justified,
            TextContainerInset = new UIEdgeInsets(top: 10, left: 15, bottom: 5, right: 5),
            BackgroundColor = Color.Transparent.ToUIColor()
        };

        txtView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
        return txtView;
    }
}

enter image description here

答案 1 :(得分:1)

  

没有环绕

因为你忘了设置Lines 0。

  

没有调整大小

我认为这是因为你在绘图时增加了宽度。

最终代码:

public override UIView GetViewForFooter(UITableView tableView, nint section)
{
    var lbl = new PaddedLabel()
    {
         Lines = 0,
        // Text = TitleForFooter(tableView, section), // or use some other text here
        Text = "Select or deselect cards from the list above and they will added or removed from the card deck",
        TextAlignment = UITextAlignment.Justified,
        LineBreakMode = UILineBreakMode.WordWrap
    };
    return lbl;
}

    public sealed class PaddedLabel : UILabel
    {
        private UIEdgeInsets EdgeInsets { get; set; } = new UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 0);

        public override void DrawText(CoreGraphics.CGRect rect)
        {
            var newRect = new CoreGraphics.CGRect(0, 0, rect.Width , rect.Height);
            base.DrawText(EdgeInsets.InsetRect(newRect));
        }
    }

enter image description here

enter image description here