From aa2a29189182a75d707ec10d31157ed5511822f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Sat, 13 Mar 2021 19:38:16 +0100 Subject: [PATCH] Support underline and strikethrough in wxMarkupToAttrString Honor wxFont's strikethrough and underline attributes when converting label markup to NSAttributedString. On macOS, strikethrough and underline are properties of (rich) text, not of fonts, so a conversion like this is necessary. --- include/wx/osx/cocoa/private/markuptoattr.h | 29 ++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/include/wx/osx/cocoa/private/markuptoattr.h b/include/wx/osx/cocoa/private/markuptoattr.h index abcdc8f92b..2a50707f1b 100644 --- a/include/wx/osx/cocoa/private/markuptoattr.h +++ b/include/wx/osx/cocoa/private/markuptoattr.h @@ -41,9 +41,7 @@ protected: // Apple documentation, attributed strings use "Helvetica 12" font by // default which is different from the system "Lucida Grande" font. So // we need to explicitly change the font for the entire string. - [m_attrString addAttribute:NSFontAttributeName - value:font.OSXGetNSFont() - range:NSMakeRange(0, [m_attrString length])]; + ApplyFont(font, NSMakeRange(0, [m_attrString length])); // Now translate the markup tags to corresponding attributes. wxMarkupParser parser(*this); @@ -58,6 +56,27 @@ protected: [m_attrString release]; } + void ApplyFont(const wxFont& font, const NSRange& range) + { + [m_attrString addAttribute:NSFontAttributeName + value:font.OSXGetNSFont() + range:range]; + + if ( font.GetStrikethrough() ) + { + [m_attrString addAttribute:NSStrikethroughStyleAttributeName + value:@(NSUnderlineStyleSingle) + range:range]; + } + + if ( font.GetUnderlined() ) + { + [m_attrString addAttribute:NSUnderlineStyleAttributeName + value:@(NSUnderlineStyleSingle) + range:range]; + } + } + // prepare text chunk for display, e.g. strip mnemonics from it virtual wxString PrepareText(const wxString& text) = 0; @@ -91,9 +110,7 @@ public: const NSRange range = NSMakeRange(start, m_pos - start); - [m_attrString addAttribute:NSFontAttributeName - value:attr.font.OSXGetNSFont() - range:range]; + ApplyFont(attr.font, range); if ( attr.foreground.IsOk() ) {