Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 4.2 #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but can be done in an independant pull request.

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file added Haring/.DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions Haring/Classes/Elements/MarkdownCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ open class MarkdownCode: MarkdownCommonElement {
attributedString.replaceCharacters(in: range, with: unescapedString)

var amendedAttributes = attributes
amendedAttributes[NSAttributedStringKey.backgroundColor] = UIColor(red: 0.91, green: 0.91, blue: 0.91, alpha: 1.0)
amendedAttributes[NSAttributedStringKey.foregroundColor] = UIColor(red: 0.95, green: 0.25, blue: 0.44, alpha: 1.0)
amendedAttributes[NSAttributedStringKey.font] = UIFont(name: "Menlo-Regular", size: 16)
amendedAttributes[.backgroundColor] = UIColor(red: 0.91, green: 0.91, blue: 0.91, alpha: 1.0)
amendedAttributes[.foregroundColor] = UIColor(red: 0.95, green: 0.25, blue: 0.44, alpha: 1.0)
amendedAttributes[.font] = UIFont(name: "Menlo-Regular", size: 16)

let newRange = (attributedString.string as NSString).range(of: unescapedString)
attributedString.addAttributes(amendedAttributes, range: NSRange(location: range.location, length: newRange.length))
Expand Down
4 changes: 2 additions & 2 deletions Haring/Classes/Elements/MarkdownHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ open class MarkdownHeader: MarkdownLevelElement {
attributedString.deleteCharacters(in: range)
}

open func attributesForLevel(_ level: Int) -> [NSAttributedStringKey: Any] {
open func attributesForLevel(_ level: Int) -> [NSAttributedString.Key: Any] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This alone will break compatibility with Swift 4.1: better also declare a compatibility layer with previous Swift version.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create a typealias like this.

#if swift(>=4.2)
    public typealias MarkdownAttribute = [NSAttributedString.Key: Any]
#else
    public typealias MarkdownAttribute = [NSAttributedStringKey: Any]
#endif

var attributes = self.attributes
if let font = font {
let headerFontSize: CGFloat = font.pointSize + (CGFloat(6 - level) * CGFloat(fontIncrease))
attributes[NSAttributedStringKey.font] = font.withSize(headerFontSize)
attributes[.font] = font.withSize(headerFontSize)
}
return attributes
}
Expand Down
2 changes: 1 addition & 1 deletion Haring/Classes/Elements/MarkdownLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class MarkdownLink: MarkdownLinkElement {
return
}
guard let url = URL(string: link) ?? URL(string: encodedLink) else { return }
attributedString.addAttribute(NSAttributedStringKey.link, value: url, range: range)
attributedString.addAttribute(.link, value: url, range: range)
}

open func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
Expand Down
4 changes: 2 additions & 2 deletions Haring/Classes/Extensions/UIFont+Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import UIKit

extension UIFont {

func withTraits(_ traits: UIFontDescriptorSymbolicTraits...) -> UIFont? {
guard let descriptor = fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) else {
func withTraits(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont? {
guard let descriptor = fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits)) else {
return nil
}
return UIFont(descriptor: descriptor, size: 0)
Expand Down
4 changes: 2 additions & 2 deletions Haring/Classes/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ open class MarkdownParser {

open func parse(_ markdown: NSAttributedString) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: markdown)
attributedString.addAttribute(NSAttributedStringKey.font, value: font,
attributedString.addAttribute(.font, value: font,
range: NSRange(location: 0, length: attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: color,
attributedString.addAttribute(.foregroundColor, value: color,
range: NSRange(location: 0, length: attributedString.length))
var elements: [MarkdownElement] = escapingElements
elements.append(contentsOf: defaultElements)
Expand Down
4 changes: 2 additions & 2 deletions Haring/Classes/Protocols/MarkdownLevelElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public protocol MarkdownLevelElement: MarkdownElement, MarkdownStyle {

func formatText(_ attributedString: NSMutableAttributedString, range: NSRange, level: Int)
func addAttributes(_ attributedString: NSMutableAttributedString, range: NSRange, level: Int)
func attributesForLevel(_ level: Int) -> [NSAttributedStringKey: Any]
func attributesForLevel(_ level: Int) -> [NSAttributedString.Key: Any]
}

public extension MarkdownLevelElement {
Expand All @@ -31,7 +31,7 @@ public extension MarkdownLevelElement {
attributedString.addAttributes(attributesForLevel(level - 1), range: range)
}

func attributesForLevel(_ level: Int) -> [NSAttributedStringKey: Any] {
func attributesForLevel(_ level: Int) -> [NSAttributedString.Key: Any] {
return self.attributes
}

Expand Down
10 changes: 5 additions & 5 deletions Haring/Classes/Protocols/MarkdownStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ public protocol MarkdownStyle {

var font: UIFont? { get }
var color: UIColor? { get }
var attributes: [NSAttributedStringKey: Any] { get }
var attributes: [NSAttributedString.Key: Any] { get }
}

public extension MarkdownStyle {

var attributes: [NSAttributedStringKey: Any] {
var attributes = [NSAttributedStringKey: Any]()
var attributes: [NSAttributedString.Key: Any] {
var attributes = [NSAttributedString.Key: Any]()
if let font = font {
attributes[NSAttributedStringKey.font] = font
attributes[.font] = font
}
if let color = color {
attributes[NSAttributedStringKey.foregroundColor] = color
attributes[.foregroundColor] = color
}
return attributes
}
Expand Down