Browse Source

Merge pull request #87 from ramonilho/feature/underline-link-style

Added LinkStyles to customize underline properties
Simon Fairbairn 5 years ago
parent
commit
bc81245371
1 changed files with 15 additions and 11 deletions
  1. 15 11
      Sources/SwiftyMarkdown/SwiftyMarkdown.swift

+ 15 - 11
Sources/SwiftyMarkdown/SwiftyMarkdown.swift

@@ -139,7 +139,10 @@ If that is not set, then the system default will be used.
     public var paragraphSpacing : CGFloat = 0.0
 }
 
-
+@objc open class LinkStyles : BasicStyles {
+    public var underlineStyle: NSUnderlineStyle = .single
+    public lazy var underlineColor: UIColor = self.color
+}
 
 /// A class that takes a [Markdown](https://daringfireball.net/projects/markdown/) string or file and returns an NSAttributedString with the applied styles. Supports Dynamic Type.
 @objc open class SwiftyMarkdown: NSObject {
@@ -226,7 +229,7 @@ If that is not set, then the system default will be used.
 	open var blockquotes = LineStyles()
 	
 	/// The styles to apply to any links found in the Markdown
-	open var link = BasicStyles()
+	open var link = LinkStyles()
 	
 	/// The styles to apply to any bold text found in the Markdown
 	open var bold = BasicStyles()
@@ -562,15 +565,16 @@ extension SwiftyMarkdown {
 				attributes[.foregroundColor] = self.bold.color
 			}
 			
-			if let linkIdx = styles.firstIndex(of: .link), linkIdx < token.metadataStrings.count {
-				attributes[.foregroundColor] = self.link.color
-				attributes[.font] = self.font(for: line, characterOverride: .link)
-				attributes[.link] = token.metadataStrings[linkIdx] as AnyObject
-				
-				if underlineLinks {
-					attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue as AnyObject
-				}
-			}
+            if let linkIdx = styles.firstIndex(of: .link), linkIdx < token.metadataStrings.count {
+                attributes[.foregroundColor] = self.link.color
+                attributes[.font] = self.font(for: line, characterOverride: .link)
+                attributes[.link] = token.metadataStrings[linkIdx] as AnyObject
+                
+                if underlineLinks {
+                    attributes[.underlineStyle] = self.link.underlineStyle.rawValue as AnyObject
+                    attributes[.underlineColor] = self.link.underlineColor
+                }
+            }
 						
 			if styles.contains(.strikethrough) {
 				attributes[.font] = self.font(for: line, characterOverride: .strikethrough)