Skip to content

Commit

Permalink
Merge pull request #27 from jjcastro/master
Browse files Browse the repository at this point in the history
Swift 4.0 compatibility
  • Loading branch information
bmoliveira authored Jan 15, 2019
2 parents 195d18f + e54dfd3 commit 46bd43d
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 29 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
4.0
11 changes: 9 additions & 2 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MarkdownKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ all the Markdown elements.
s.source = { :git => 'https://github.com/ivanbruel/MarkdownKit.git', :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/ivanbruel'

s.ios.deployment_target = '8.0'
s.ios.deployment_target = '9.0'

s.source_files = 'MarkdownKit/Classes/**/*'
s.frameworks = 'UIKit'
Expand Down
Binary file modified MarkdownKit/.DS_Store
Binary file not shown.
Binary file modified MarkdownKit/Classes/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class MarkdownCodeEscaping: MarkdownElement {
}

open func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
let range = match.rangeAt(2)
let range = match.range(at: 2)
// escaping all characters
let matchString = attributedString.attributedSubstring(from: range).string
let escapedString = Array<UInt16>(matchString.utf16)
Expand Down
6 changes: 3 additions & 3 deletions MarkdownKit/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) -> [String: AnyObject] {
open func attributesForLevel(_ level: Int) -> [NSAttributedString.Key: AnyObject] {
var attributes = self.attributes
if let font = font {
let headerFontSize: CGFloat = font.pointSize + (CGFloat(level) * CGFloat(fontIncrease))
attributes[NSFontAttributeName] = font.withSize(headerFontSize)
let headerFontSize: CGFloat = font.pointSize + 4 + (-1 * CGFloat(level) * CGFloat(fontIncrease))
attributes[NSAttributedString.Key.font] = font.withSize(headerFontSize).bold()
}
return attributes
}
Expand Down
2 changes: 1 addition & 1 deletion MarkdownKit/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(NSLinkAttributeName, value: url, range: range)
attributedString.addAttribute(NSAttributedString.Key.link, value: url, range: range)
}

open func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
Expand Down
3 changes: 1 addition & 2 deletions MarkdownKit/Classes/Extensions/String+UTF16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ extension String {
stride(from: 0, to: characters.count, by: 4).forEach {
let startIndex = characters.index(characters.startIndex, offsetBy: $0)
let endIndex = characters.index(characters.startIndex, offsetBy: $0 + 4)
let hex4 = substring(with: startIndex..<endIndex)

let hex4 = String(self[startIndex..<endIndex])
if let utf16 = UInt16(hex4, radix: 16) {
utf16Array.append(utf16)
}
Expand Down
4 changes: 2 additions & 2 deletions MarkdownKit/Classes/Extensions/UIFont+Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import UIKit

extension UIFont {

func withTraits(_ traits: UIFontDescriptorSymbolicTraits...) -> UIFont {
func withTraits(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
let descriptor = fontDescriptor
.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits))
return UIFont(descriptor: descriptor!, size: 0)
}

Expand Down
2 changes: 1 addition & 1 deletion MarkdownKit/Classes/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ open class MarkdownParser {

open func parse(_ markdown: NSAttributedString) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: markdown)
attributedString.addAttribute(NSFontAttributeName, value: font,
attributedString.addAttribute(NSAttributedString.Key.font, value: font,
range: NSRange(location: 0, length: attributedString.length))
var elements: [MarkdownElement] = escapingElements
elements.append(contentsOf: defaultElements)
Expand Down
6 changes: 3 additions & 3 deletions MarkdownKit/Classes/Protocols/MarkdownCommonElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public extension MarkdownCommonElement {

func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
// deleting trailing markdown
attributedString.deleteCharacters(in: match.rangeAt(4))
attributedString.deleteCharacters(in: match.range(at: 4))
// formatting string (may alter the length)
addAttributes(attributedString, range: match.rangeAt(3))
addAttributes(attributedString, range: match.range(at: 3))
// deleting leading markdown
attributedString.deleteCharacters(in: match.rangeAt(2))
attributedString.deleteCharacters(in: match.range(at: 2))
}
}
12 changes: 6 additions & 6 deletions MarkdownKit/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) -> [String: AnyObject]
func attributesForLevel(_ level: Int) -> [NSAttributedString.Key: AnyObject]
}

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

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

func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
let level = match.rangeAt(1).length
addAttributes(attributedString, range: match.rangeAt(2), level: level)
let range = NSRange(location: match.rangeAt(1).location,
length: match.rangeAt(2).location - match.rangeAt(1).location)
let level = match.range(at: 1).length
addAttributes(attributedString, range: match.range(at: 2), level: level)
let range = NSRange(location: match.range(at: 1).location,
length: match.range(at: 2).location - match.range(at: 1).location)
formatText(attributedString, range: range, level: level)
}
}
12 changes: 6 additions & 6 deletions MarkdownKit/Classes/Protocols/MarkdownStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ public protocol MarkdownStyle {

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

public extension MarkdownStyle {

var attributes: [String: AnyObject] {
var attributes = [String: AnyObject]()
var attributes: [NSAttributedString.Key: AnyObject] {
var attributes = [NSAttributedString.Key: AnyObject]()
if let font = font {
attributes[NSFontAttributeName] = font
attributes[NSAttributedString.Key.font] = font
}
if let color = color {
attributes[NSForegroundColorAttributeName] = color
attributes[NSAttributedString.Key.foregroundColor] = color
}
return attributes
}

}
}

0 comments on commit 46bd43d

Please sign in to comment.