From b01c3d9b098e66033501f3c082a36b9cb12e36dd Mon Sep 17 00:00:00 2001 From: Peter <> Date: Fri, 11 Oct 2019 16:10:08 +0400 Subject: [PATCH] Support markdown in standardTextAlertController --- .../Display/Display/TextAlertController.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/submodules/Display/Display/TextAlertController.swift b/submodules/Display/Display/TextAlertController.swift index a302bb0611..2f88965a46 100644 --- a/submodules/Display/Display/TextAlertController.swift +++ b/submodules/Display/Display/TextAlertController.swift @@ -1,6 +1,7 @@ import Foundation import UIKit import AsyncDisplayKit +import Markdown private let alertWidth: CGFloat = 270.0 @@ -354,9 +355,19 @@ public func textAlertController(theme: AlertControllerTheme, title: NSAttributed return AlertController(theme: theme, contentNode: TextAlertContentNode(theme: theme, title: title, text: text, actions: actions, actionLayout: actionLayout)) } -public func standardTextAlertController(theme: AlertControllerTheme, title: String?, text: String, actions: [TextAlertAction], actionLayout: TextAlertContentActionLayout = .horizontal, allowInputInset: Bool = true) -> AlertController { +public func standardTextAlertController(theme: AlertControllerTheme, title: String?, text: String, actions: [TextAlertAction], actionLayout: TextAlertContentActionLayout = .horizontal, allowInputInset: Bool = true, parseMarkdown: Bool = false) -> AlertController { var dismissImpl: (() -> Void)? - let controller = AlertController(theme: theme, contentNode: TextAlertContentNode(theme: theme, title: title != nil ? NSAttributedString(string: title!, font: Font.semibold(17.0), textColor: theme.primaryColor, paragraphAlignment: .center) : nil, text: NSAttributedString(string: text, font: title == nil ? Font.semibold(17.0) : Font.regular(13.0), textColor: theme.primaryColor, paragraphAlignment: .center), actions: actions.map { action in + let attributedText: NSAttributedString + if parseMarkdown { + let font = title == nil ? Font.semibold(17.0) : Font.regular(13.0) + let boldFont = title == nil ? Font.bold(17.0) : Font.semibold(13.0) + let body = MarkdownAttributeSet(font: font, textColor: theme.primaryColor) + let bold = MarkdownAttributeSet(font: boldFont, textColor: theme.primaryColor) + attributedText = parseMarkdownIntoAttributedString(text, attributes: MarkdownAttributes(body: body, bold: bold, link: body, linkAttribute: { _ in nil }), textAlignment: .center) + } else { + attributedText = NSAttributedString(string: text, font: title == nil ? Font.semibold(17.0) : Font.regular(13.0), textColor: theme.primaryColor, paragraphAlignment: .center) + } + let controller = AlertController(theme: theme, contentNode: TextAlertContentNode(theme: theme, title: title != nil ? NSAttributedString(string: title!, font: Font.semibold(17.0), textColor: theme.primaryColor, paragraphAlignment: .center) : nil, text: attributedText, actions: actions.map { action in return TextAlertAction(type: action.type, title: action.title, action: { dismissImpl?() action.action()