我正在尝试在MvxTableViewCell的UILabel上设置下划线。

    [Register("KittenCell")]
    public class KittenCell : MvxTableViewCell
    {
        private UIImageView MainImage;
        private UILabel NameLabel;
        private UILabel PriceValueLabel;
        private UILabel PriceLabel;

        public static readonly NSString Key = new NSString ("KittenCell");
        private MvxImageViewLoader imageLoader;

        public KittenCell ()
        {
            CreateLayout ();
            InitializeBindings ();
        }

        public KittenCell(IntPtr handle)
            : base(handle)
        {
            CreateLayout ();
            InitializeBindings ();
        }

        void CreateLayout ()
        {
            MainImage = new UIImageView (new RectangleF (0, 0, 160, 100));

            NameLabel = new UILabel (new RectangleF (168, 15, 144, 21));
            NameLabel.TextAlignment = UITextAlignment.Left;
            var attrs = new UIStringAttributes {
                UnderlineStyle = NSUnderlineStyle.Single
            };
            NameLabel.AttributedText =new NSAttributedString(NameLabel.Text,attrs);

            PriceLabel = new UILabel (new RectangleF (168, 59, 57, 21));
            PriceLabel.Text = "Price:";
            PriceLabel.TextAlignment = UITextAlignment.Left;
            PriceValueLabel = new UILabel (new RectangleF (228, 59, 84, 21));
            PriceValueLabel.TextAlignment = UITextAlignment.Left;
            PriceValueLabel.TextColor = UIColor.Blue;
            ContentView.AddSubviews (MainImage, NameLabel, PriceLabel, PriceValueLabel);
        }

        void InitializeBindings ()
        {
            imageLoader = new MvxImageViewLoader (() => this.MainImage);

            this.DelayBind (() => {
                var set = this.CreateBindingSet<KittenCell, Kitten> ();
                set.Bind (NameLabel).To(kitten => kitten.Name);
                set.Bind (PriceValueLabel).To(kitten => kitten.Price);
                set.Bind (imageLoader).To(kitten => kitten.ImageUrl);
                set.Apply ();
            });

        }
    }


但是显示UILabel时,其下没有下划线。我的解释是,下划线适用于尚未绑定的文本。但是绑定后如何设置呢?

最佳答案

您可以使用转换器:

public class UnderlineTextValueConverter : MvxValueConverter<string, NSAttributedString>
    {
        protected override NSAttributedString Convert(string value, Type targetType, object parameter, CultureInfo cultureInfo)
        {
            var attrs = new UIStringAttributes {
                UnderlineStyle = NSUnderlineStyle.Single
            };
            return new NSAttributedString(value, attrs);
        }
    }


然后更新您对此的绑定...

set.Bind (NameLabel).For(l => l.AttributedText).To(kitten => kitten.Name).WithConversion("UnderlineText", null);


上面的代码未经测试,但是应该不需任何改动就可以工作。

关于c# - MvxTableViewCell上的AttributedText,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30185783/

10-12 04:07