MvvmCross iOS 带有语言转换器的 UILabel AttributedText

MvvmCross iOS UILabel AttributedText with Language converter

我使用自定义 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() + "");            
}

有什么解决办法吗?

我将 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();
        }

出现下划线,这是你想要的吗?

我只是将 Initialize 方法放入我的自定义标签的 Draw 中。

解决方法如下:

 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
                });
 }