Skip to content

Commit

Permalink
Make it possible to customize the link attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej Mihajlov committed Nov 8, 2023
1 parent d3bde6e commit 069ea6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions ios/MullvadVPN/Classes/Markdown/MarkdownStylingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
2 changes: 1 addition & 1 deletion ios/MullvadVPNTests/MarkdownParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 069ea6b

Please sign in to comment.