diff --git a/ios/MullvadVPN/Classes/Markdown/AttributedStringSupport/MarkdownLinkNode+AttributedString.swift b/ios/MullvadVPN/Classes/Markdown/AttributedStringSupport/MarkdownLinkNode+AttributedString.swift index 9473e4da2351..2725194fdd56 100644 --- a/ios/MullvadVPN/Classes/Markdown/AttributedStringSupport/MarkdownLinkNode+AttributedString.swift +++ b/ios/MullvadVPN/Classes/Markdown/AttributedStringSupport/MarkdownLinkNode+AttributedString.swift @@ -10,7 +10,7 @@ import Foundation extension MarkdownLinkNode: AttributedMarkdown { func attributedString(options: MarkdownStylingOptions, applyEffect: MarkdownEffectCallback?) -> NSAttributedString { - var attributes: [NSAttributedString.Key: Any] = [.font: options.font, .hyperlink: url] + var attributes: [NSAttributedString.Key: Any] = [.font: options.font, options.linkAttribute.attributeKey: url] if let linkColor = options.linkColor { attributes[.foregroundColor] = linkColor diff --git a/ios/MullvadVPN/Classes/Markdown/MarkdownStylingOptions.swift b/ios/MullvadVPN/Classes/Markdown/MarkdownStylingOptions.swift index aa01b696d21e..0605ccdeead1 100644 --- a/ios/MullvadVPN/Classes/Markdown/MarkdownStylingOptions.swift +++ b/ios/MullvadVPN/Classes/Markdown/MarkdownStylingOptions.swift @@ -8,14 +8,46 @@ import UIKit +/// Struct describing the visual style that should be used when converting from markdown to attributed string. struct MarkdownStylingOptions { + /// Primary font for text. var font: UIFont + + /// Text color. var textColor: UIColor? + + /// The color of the link. + /// UIKit controls may ignore it when used with standard link attributes. var linkColor: UIColor? + + /// The attribute that holds the URL. + var linkAttribute: MarkdownLinkAttribute = .standard + + /// Paragraph style var paragraphStyle: NSParagraphStyle = .default + /// Bold font derived from primary font. var boldFont: UIFont { let fontDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) ?? font.fontDescriptor return UIFont(descriptor: fontDescriptor, size: font.pointSize) } } + +/// The attribute that holds the URL. +enum MarkdownLinkAttribute { + /// Standard `NSLinkAttribute` attribute. + case standard + + /// Custom hyperlink attribute. + case custom + + /// Returns the attribute key which should be used to store the link URL. + var attributeKey: NSAttributedString.Key { + switch self { + case .standard: + return .link + case .custom: + return .hyperlink + } + } +} diff --git a/ios/MullvadVPNTests/MarkdownParserTests.swift b/ios/MullvadVPNTests/MarkdownParserTests.swift index 032580fdc42a..2a001bbabcbd 100644 --- a/ios/MullvadVPNTests/MarkdownParserTests.swift +++ b/ios/MullvadVPNTests/MarkdownParserTests.swift @@ -178,7 +178,7 @@ private extension MarkdownParserTests { func link(title: String, url: String) -> NSAttributedString { var attributes: [NSAttributedString.Key: Any] = [ .font: defaultStylingOptions.font, - .hyperlink: url, + .link: url, ] if let linkColor = defaultStylingOptions.linkColor { attributes[.foregroundColor] = linkColor