private void ShowTip()
 {
     using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Tiled2UnityMac.Resources.LaunchTipMac.rtf")) {
         var          rtfData          = NSData.FromStream(stream);
         NSDictionary nsDict           = new NSDictionary();
         var          attributedString = NSAttributedString.CreateWithRTF(rtfData, out nsDict);
         this.TextFieldLaunchTip.AttributedStringValue = attributedString;
     }
 }
Example #2
0
        public void Load(Stream stream, RichTextAreaFormat format)
        {
            var range = new NSRange(0, Control.TextStorage.Length);

            switch (format)
            {
            case RichTextAreaFormat.Rtf:
            {
                var str = NSAttributedString.CreateWithRTF(NSData.FromStream(stream), out var docAttributes);
                NSMutableAttributedString mut = null;
                nint pos = 0;
                // when encountering an RTF without a defined foreground, use the system foreground for dark mode
                var textColor = TextColor.ToNSUI();
                while (pos < str.Length)
                {
                    var color = str.GetAttribute(NSStringAttributeKey.ForegroundColor, pos, out var effectiveRange);
                    pos = effectiveRange.Location + effectiveRange.Length;
                    if (color == null)
                    {
                        if (mut == null)
                        {
                            mut = new NSMutableAttributedString(str);
                        }

                        mut.AddAttribute(NSStringAttributeKey.ForegroundColor, textColor, effectiveRange);
                    }
                }

                if (mut != null)
                {
                    str = mut;
                }

                Control.TextStorage.Replace(range, str);
                break;
            }

            case RichTextAreaFormat.PlainText:
            {
                var str = new NSMutableAttributedString(new StreamReader(stream).ReadToEnd());
                Font.Apply(str);
                str.AddAttribute(NSStringAttributeKey.ForegroundColor, TextColor.ToNSUI(), new NSRange(0, str.Length));
                Control.TextStorage.Replace(range, str);
                break;
            }

            default:
                throw new NotSupportedException();
            }
            Callback.OnTextChanged(Widget, EventArgs.Empty);
        }