diff options
author | Dmitriy K <dmitriy.kirakosyan@gmail.com> | 2019-02-15 16:26:02 +0300 |
---|---|---|
committer | Dmitriy K <dmitriy.kirakosyan@gmail.com> | 2019-02-16 23:11:05 +0300 |
commit | a416b0617c8384d1292d56b627d15a49c391e6a4 (patch) | |
tree | d06efe33b380dbbb239b3e5c4379f57bd98d548e | |
parent | 8612ac3ebd859f8b6e0009f56c93e1b12309cb53 (diff) |
[XamMac] Fix NSTextView text color bug.
Setting the text color to its previous value after applying
an attributed string. Otherwise the text color resets to Black
no matter of what color it was before.
VSTS Bug: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/612427
-rw-r--r-- | Xwt.XamMac/Xwt.Mac/RichTextViewBackend.cs | 5 | ||||
-rw-r--r-- | Xwt.XamMac/Xwt.Mac/Util.cs | 24 |
2 files changed, 25 insertions, 4 deletions
diff --git a/Xwt.XamMac/Xwt.Mac/RichTextViewBackend.cs b/Xwt.XamMac/Xwt.Mac/RichTextViewBackend.cs index ec918292..facc0838 100644 --- a/Xwt.XamMac/Xwt.Mac/RichTextViewBackend.cs +++ b/Xwt.XamMac/Xwt.Mac/RichTextViewBackend.cs @@ -148,7 +148,7 @@ namespace Xwt.Mac lineSpacing = value; if (currentBuffer != null) - Widget.TextStorage.SetString (currentBuffer.ToAttributedString (font, lineSpacing)); + Widget.SetAttributedString (currentBuffer.ToAttributedString (font, lineSpacing)); } } @@ -173,7 +173,8 @@ namespace Xwt.Mac if (tview == null) return; - tview.TextStorage.SetString (macBuffer.ToAttributedString (font, lineSpacing)); + + tview.SetAttributedString (macBuffer.ToAttributedString (font, lineSpacing)); } public override void EnableEvent (object eventId) diff --git a/Xwt.XamMac/Xwt.Mac/Util.cs b/Xwt.XamMac/Xwt.Mac/Util.cs index f9c3290c..f7a07950 100644 --- a/Xwt.XamMac/Xwt.Mac/Util.cs +++ b/Xwt.XamMac/Xwt.Mac/Util.cs @@ -58,6 +58,28 @@ namespace Xwt.Mac } } + public static void SetAttributedString (this NSTextView view, NSAttributedString str) + { + var textColor = view.TextColor; + view.TextStorage.SetString (str); + + // Workaround: + // Check if we have ForegroundColor attribute + // And if we don't, then apply the previous view's TextColor, + // otherwise it would be reset to Black by the line above. + var hasForegroundAttr = false; + view.TextStorage.EnumerateAttributes (new NSRange (0, view.TextStorage.Length), NSAttributedStringEnumeration.None, (NSDictionary attrs, NSRange range, ref bool stop) => { + stop = false; + if (attrs.ContainsKey (NSStringAttributeKey.ForegroundColor)) { + hasForegroundAttr = true; + stop = true; + } + }); + + if (!hasForegroundAttr && textColor != null) + view.TextColor = textColor; + } + public static double WidgetX (this NSView v) { return (double) v.Frame.X; @@ -408,8 +430,6 @@ namespace Xwt.Mac return ns; } - - public static NSMutableAttributedString WithAlignment (this NSMutableAttributedString ns, NSTextAlignment alignment) { if (ns == null) |