【问题标题】:Text overlay on Image in Xamarin form using PCL使用 PCL 以 Xamarin 形式在图像上叠加文本
【发布时间】:2016-09-18 04:46:35
【问题描述】:

我需要使用 PCL 项目在 Xamarin 形式的图像上进行文本叠加,如下图所示

我创建了replative布局来做到这一点,谁能建议我如何格式化文本?

 var myLabel = new Label()
        {
            Text = "Hello World",
            Font = Font.SystemFontOfSize(20),
            TextColor = Color.White,
            XAlign = TextAlignment.Center,
            YAlign = TextAlignment.Center
        };

        var stack = new StackLayout
        {
            Children = {myLabel}
        };

        var slayout = new ContentView
        {
            BackgroundColor = new Color(0,0,0,.5),
            Content = stack
        };

        var myImage = new Image()
        {
            Source = "deal1.png"
        };

        RelativeLayout layout = new RelativeLayout();

        layout.Children.Add(myImage,
            Constraint.Constant(0),
            Constraint.Constant(0),
            Constraint.RelativeToParent((parent) => { return parent.Width; }),
            Constraint.RelativeToParent((parent) => { return parent.Height; }));

        layout.Children.Add(slayout,
            Constraint.Constant(0),
            Constraint.Constant(0),
            Constraint.RelativeToParent((parent) => { return parent.Width; }),
            Constraint.RelativeToParent((parent) => { return parent.Height; }));

【问题讨论】:

  • 不清楚你在问什么 - 布局或标签格式?
  • 我需要在图片上添加文字叠加,如附件中所示。
  • 不确定,但也许您应该使用 AbsoluteLayout 而不是 RelativeLayout?
  • @FredyWenger- 你有任何示例源代码吗?

标签: xamarin.forms


【解决方案1】:

我会写一些看起来像这样的东西(使用更多可绑定属性和标签来自定义覆盖文本的不同元素,以及标签背景颜色和不透明度的可绑定属性)。

public class OverlayedImage : ContentView
    {
        public static readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ImageSource), typeof(OverlayedImage));
        public ImageSource Source
        {
            get { return (ImageSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(OverlayedImage));
        public string Text
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        protected override void OnPropertyChanged(string propertyName = null)
        {
            if (propertyName == SourceProperty.PropertyName)
            {
                _image.Source = Source;
            }
            else if (propertyName == TextProperty.PropertyName)
            {
                _label.Text = Text;
            }
            else
                base.OnPropertyChanged(propertyName);
        }


        private readonly Image _image =new Image();
        private readonly Label _label = new Label();

        public OverlayedImage()
        {
            var abs = new AbsoluteLayout();
            abs.Children.Add(_image, new Rectangle(0, 0 , 1 , 1), AbsoluteLayoutFlags.All);
            abs.Children.Add(_label, new Rectangle(0, 1, 1, 0.5), AbsoluteLayoutFlags.All);
            Content = abs;
        }

    }

【讨论】:

  • 如何在堆栈布局中使用这个?
  • 像任何其他元素一样。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多