MvvmCross iOS UILabel AttributedText与语言转换器

时间:2018-10-21 19:59:26

标签: xamarin xamarin.ios mvvmcross

我使用自定义Kern参数为UILabel创建自定义控件。

 [Register("CustomLabelView"), DesignTimeVisible(true)]
    public class CustomLabelView : UILabel
    {
        private float _textLetterSpacing;

        public CustomLabelView(IntPtr handle) : base(handle)
        {
        }

        [Export("TextLetterSpacing"), Browsable(true)]
        public float TextLetterSpacing
        {
            get => _textLetterSpacing;
            set
            {
                _textLetterSpacing = value;
                SetNeedsDisplay();
            }
        }

        public CustomLabelView()
        {
            Initialize();
        }

        public override void AwakeFromNib()
        {
            // Called when loaded from xib or storyboard.
            Initialize();
        }

        void Initialize()
        {
            NSRange range = new NSRange(0, Text.Length);
            var attributes = AttributedText.GetCoreTextAttributes(0, out range);
            var attributedTitle = new NSAttributedString(Text, new CTStringAttributes() { KerningAdjustment = TextLetterSpacing, ForegroundColor = TextColor.CGColor, UnderlineStyle = attributes.UnderlineStyle });
            AttributedText = attributedTitle;
            SetNeedsDisplay();
        }
    }

我使用language converter

将文本绑定到此控件
set.Bind(CustomLabelView)
   .For(c => c.Text)
   .To(vm => vm.TextSource)
   .WithConversion("Language", "AgreementText");

underline属性不适用于绑定文本。我也尝试使用AttributedText属性而不是Text。没有任何变化。

我的 LinkerPleaseInclude 文件包含以下行

public void Include(UILabel label)
{
  label.Text = label.Text + "";
  label.AttributedText = new NSAttributedString(label.AttributedText.ToString() + "");            
}

任何想法如何解决?

2 个答案:

答案 0 :(得分:0)

我将UnderlineStyle属性的值更改为CTUnderlineStyle枚举之一,它对我有用。

namespace CoreText
    {
        //
        // Summary:
        //     Specifies the style of an underline ornament.
        //
        // Remarks:
        //     To be added.
        public enum CTUnderlineStyle
        {
            None = 0,
            Single = 1,
            Thick = 2,
            Double = 9
        }
    }

这是我的代码:

 void Initialize()
        {
            this.Text = "This is some formatted text";
            NSRange range = new NSRange(0, Text.Length);
            var attributes = AttributedText.GetCoreTextAttributes(0, out range);
            var attributedTitle = new NSAttributedString(Text, new CTStringAttributes() { KerningAdjustment = TextLetterSpacing, ForegroundColor = TextColor.CGColor, UnderlineStyle = CTUnderlineStyle.Single });
            AttributedText = attributedTitle;
            SetNeedsDisplay();
        }

出现下划线,您要这个吗?

答案 1 :(得分:0)

我只是将Initialize方法放入了Draw中用于我的CustomLabel。

这是解决方案:

 public override void Draw(CGRect rect)
 {
     Initialize();
     base.Draw(rect);
 }

 private void Initialize()
 {
     var attributes = AttributedText.GetCoreTextAttributes(0, out _);
     AttributedText = new NSMutableAttributedString(Text,
                new CTStringAttributes
                {
                    KerningAdjustment = TextLetterSpacing, Font = attributes.Font,
                    UnderlineStyle = attributes.UnderlineStyle
                });
 }