Skip to content

Commit

Permalink
Convert to Swift 2
Browse files Browse the repository at this point in the history
  • Loading branch information
indragiek committed Jan 11, 2016
1 parent c26a469 commit cf4ca55
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.indragie.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
15 changes: 7 additions & 8 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class ViewController: UIViewController {

let attributes = MarkdownAttributes()
let textStorage = MarkdownTextStorage(attributes: attributes)
var error: NSError?
if let linkHighlighter = LinkHighlighter(errorPtr: &error) {
textStorage.addHighlighter(linkHighlighter)
} else {
assertionFailure("Error initializing LinkHighlighter: \(error)")
do {
textStorage.addHighlighter(try LinkHighlighter())
} catch let error {
fatalError("Error initializing LinkHighlighter: \(error)")
}
textStorage.addHighlighter(MarkdownStrikethroughHighlighter())
textStorage.addHighlighter(MarkdownSuperscriptHighlighter())
Expand All @@ -28,12 +27,12 @@ class ViewController: UIViewController {
}

let textView = MarkdownTextView(frame: CGRectZero, textStorage: textStorage)
textView.setTranslatesAutoresizingMaskIntoConstraints(false)
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)

let views = ["textView": textView]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[textView]-20-|", options: nil, metrics: nil, views: views)
constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[textView]-20-|", options: nil, metrics: nil, views: views)
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[textView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[textView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
NSLayoutConstraint.activateConstraints(constraints)
}
}
11 changes: 10 additions & 1 deletion MarkdownTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@
7252E0241AF1BC1D006D0535 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0630;
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
ORGANIZATIONNAME = "Indragie Karunaratne";
TargetAttributes = {
7252E02C1AF1BC1D006D0535 = {
Expand Down Expand Up @@ -442,6 +443,7 @@
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -520,6 +522,7 @@
INFOPLIST_FILE = MarkdownTextView/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.indragie.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -537,6 +540,7 @@
INFOPLIST_FILE = MarkdownTextView/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.indragie.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
};
Expand All @@ -555,6 +559,7 @@
);
INFOPLIST_FILE = MarkdownTextViewTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.indragie.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -568,6 +573,7 @@
);
INFOPLIST_FILE = MarkdownTextViewTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.indragie.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -582,6 +588,7 @@
);
INFOPLIST_FILE = Example/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.indragie.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -592,6 +599,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Example/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.indragie.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down Expand Up @@ -633,6 +641,7 @@
7252E0871AF1BD61006D0535 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
8 changes: 4 additions & 4 deletions MarkdownTextView/HighlighterTextStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class HighlighterTextStorage: NSTextStorage {
super.init()
}

required public init(coder aDecoder: NSCoder) {
required public init?(coder aDecoder: NSCoder) {
backingStore = NSMutableAttributedString(string: "", attributes: defaultAttributes)
super.init(coder: aDecoder)
}
Expand All @@ -55,7 +55,7 @@ public class HighlighterTextStorage: NSTextStorage {
return backingStore.string
}

public override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [NSObject : AnyObject] {
public override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
return backingStore.attributesAtIndex(location, effectiveRange: range)
}

Expand All @@ -64,7 +64,7 @@ public class HighlighterTextStorage: NSTextStorage {
edited(.EditedCharacters, range: range, changeInLength: attrString.length - range.length)
}

public override func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange) {
public override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
backingStore.setAttributes(attrs, range: range)
edited(.EditedAttributes, range: range, changeInLength: 0)
}
Expand All @@ -87,7 +87,7 @@ public class HighlighterTextStorage: NSTextStorage {
private func highlightRange(range: NSRange) {
backingStore.beginEditing()
setAttributes(defaultAttributes, range: range)
var attrString = backingStore.attributedSubstringFromRange(range).mutableCopy() as! NSMutableAttributedString
let attrString = backingStore.attributedSubstringFromRange(range).mutableCopy() as! NSMutableAttributedString
for highlighter in highlighters {
highlighter.highlightAttributedString(attrString)
}
Expand Down
2 changes: 1 addition & 1 deletion MarkdownTextView/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.indragie.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
14 changes: 3 additions & 11 deletions MarkdownTextView/LinkHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@ import UIKit
public final class LinkHighlighter: HighlighterType {
private var detector: NSDataDetector!

public init?(errorPtr: NSErrorPointer) {
var error: NSError?
if let detector = NSDataDetector(types: NSTextCheckingType.Link.rawValue, error: &error) {
self.detector = detector
} else {
if (errorPtr != nil) {
errorPtr.memory = error
}
return nil
}
public init() throws {
detector = try NSDataDetector(types: NSTextCheckingType.Link.rawValue)
}

// MARK: HighlighterType

public func highlightAttributedString(attributedString: NSMutableAttributedString) {
enumerateMatches(detector, attributedString.string) {
enumerateMatches(detector, string: attributedString.string) {
if let URL = $0.URL {
let linkAttributes = [
NSLinkAttributeName: URL
Expand Down
4 changes: 2 additions & 2 deletions MarkdownTextView/MarkdownAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public struct MarkdownAttributes {
]

public var orderedListAttributes: TextAttributes? = [
NSFontAttributeName: fontWithTraits(.TraitBold, UIFont.preferredFontForTextStyle(UIFontTextStyleBody))
NSFontAttributeName: fontWithTraits(.TraitBold, font: UIFont.preferredFontForTextStyle(UIFontTextStyleBody))
]

public var orderedListItemAttributes: TextAttributes? = [
Expand All @@ -90,7 +90,7 @@ public struct MarkdownAttributes {
]

public var unorderedListAttributes: TextAttributes? = [
NSFontAttributeName: fontWithTraits(.TraitBold, UIFont.preferredFontForTextStyle(UIFontTextStyleBody))
NSFontAttributeName: fontWithTraits(.TraitBold, font: UIFont.preferredFontForTextStyle(UIFontTextStyleBody))
]

public var unorderedListItemAttributes: TextAttributes? = [
Expand Down
2 changes: 1 addition & 1 deletion MarkdownTextView/MarkdownHeaderHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class MarkdownHeaderHighlighter: HighlighterType {
// MARK: HighlighterType

public func highlightAttributedString(attributedString: NSMutableAttributedString) {
enumerateMatches(self.dynamicType.HeaderRegex, attributedString.string) {
enumerateMatches(self.dynamicType.HeaderRegex, string: attributedString.string) {
let level = $0.rangeAtIndex(1).length
if let attributes = self.attributes.attributesForHeaderLevel(level) {
attributedString.addAttributes(attributes, range: $0.range)
Expand Down
2 changes: 1 addition & 1 deletion MarkdownTextView/MarkdownLinkHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class MarkdownLinkHighlighter: HighlighterType {

public func highlightAttributedString(attributedString: NSMutableAttributedString) {
let string = attributedString.string
enumerateMatches(self.dynamicType.LinkRegex, string) {
enumerateMatches(self.dynamicType.LinkRegex, string: string) {
let URLString = (string as NSString).substringWithRange($0.rangeAtIndex(2))
let linkAttributes = [
NSLinkAttributeName: URLString
Expand Down
2 changes: 1 addition & 1 deletion MarkdownTextView/MarkdownListHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class MarkdownListHighlighter: HighlighterType {
public func highlightAttributedString(attributedString: NSMutableAttributedString) {
if (attributes == nil && itemAttributes == nil) { return }

enumerateMatches(regularExpression, attributedString.string) {
enumerateMatches(regularExpression, string: attributedString.string) {
if let attributes = self.attributes {
attributedString.addAttributes(attributes, range: $0.range)
}
Expand Down
2 changes: 1 addition & 1 deletion MarkdownTextView/MarkdownStrikethroughHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class MarkdownStrikethroughHighlighter: HighlighterType {
// MARK: HighlighterType

public func highlightAttributedString(attributedString: NSMutableAttributedString) {
enumerateMatches(self.dynamicType.StrikethroughRegex, attributedString.string) {
enumerateMatches(self.dynamicType.StrikethroughRegex, string: attributedString.string) {
var strikethroughAttributes: TextAttributes = [
NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
]
Expand Down
6 changes: 3 additions & 3 deletions MarkdownTextView/MarkdownSuperscriptHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public final class MarkdownSuperscriptHighlighter: HighlighterType {
var previousRange: NSRange?
var level: Int = 0

enumerateMatches(self.dynamicType.SuperscriptRegex, attributedString.string) {
enumerateMatches(self.dynamicType.SuperscriptRegex, string: attributedString.string) {
level += $0.rangeAtIndex(1).length
let textRange = $0.range
let attributes = attributedString.attributesAtIndex(textRange.location, effectiveRange: nil) as! TextAttributes
let attributes = attributedString.attributesAtIndex(textRange.location, effectiveRange: nil)

let isConsecutiveRange: Bool = {
if let previousRange = previousRange where NSMaxRange(previousRange) == textRange.location {
Expand All @@ -48,7 +48,7 @@ public final class MarkdownSuperscriptHighlighter: HighlighterType {
level++
}

attributedString.addAttributes(superscriptAttributes(attributes, level, self.fontSizeRatio), range: textRange)
attributedString.addAttributes(superscriptAttributes(attributes, level: level, ratio: self.fontSizeRatio), range: textRange)
previousRange = textRange

if !isConsecutiveRange {
Expand Down
4 changes: 2 additions & 2 deletions MarkdownTextView/MarkdownTextStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class MarkdownTextStorage: HighlighterTextStorage {
addPattern("(`+)(?:.+?)(?<!`)\\1(?!`)", attributes.inlineCodeAttributes)
}

required public init(coder aDecoder: NSCoder) {
required public init?(coder aDecoder: NSCoder) {
attributes = MarkdownAttributes()
super.init(coder: aDecoder)
commonInit()
Expand All @@ -82,7 +82,7 @@ public class MarkdownTextStorage: HighlighterTextStorage {
private func attributesForTraits(traits: UIFontDescriptorSymbolicTraits, var _ attributes: TextAttributes?) -> TextAttributes? {
if let defaultFont = defaultAttributes[NSFontAttributeName] as? UIFont where attributes == nil {
attributes = [
NSFontAttributeName: fontWithTraits(traits, defaultFont)
NSFontAttributeName: fontWithTraits(traits, font: defaultFont)
]
}
return attributes
Expand Down
2 changes: 1 addition & 1 deletion MarkdownTextView/RegularExpressionHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RegularExpressionHighlighter: HighlighterType {
// MARK: HighlighterType

public func highlightAttributedString(attributedString: NSMutableAttributedString) {
enumerateMatches(regularExpression, attributedString.string) {
enumerateMatches(regularExpression, string: attributedString.string) {
attributedString.addAttributes(self.attributes, range: $0.range)
}
}
Expand Down
23 changes: 11 additions & 12 deletions MarkdownTextView/TextUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ import UIKit
public typealias TextAttributes = [String: AnyObject]

internal func fontWithTraits(traits: UIFontDescriptorSymbolicTraits, font: UIFont) -> UIFont {
let combinedTraits = UIFontDescriptorSymbolicTraits(font.fontDescriptor().symbolicTraits.rawValue | (traits.rawValue & 0xFFFF))
if let descriptor = font.fontDescriptor().fontDescriptorWithSymbolicTraits(combinedTraits) {
return UIFont(descriptor: descriptor, size: font.pointSize)
}
return font
let combinedTraits = UIFontDescriptorSymbolicTraits(rawValue: font.fontDescriptor().symbolicTraits.rawValue | (traits.rawValue & 0xFFFF))
let descriptor = font.fontDescriptor().fontDescriptorWithSymbolicTraits(combinedTraits)
return UIFont(descriptor: descriptor, size: font.pointSize)
}

internal func regexFromPattern(pattern: String) -> NSRegularExpression {
var error: NSError?
if let regex = NSRegularExpression(pattern: pattern, options: .AnchorsMatchLines, error: &error) {
return regex
} else {
fatalError("Failed to initialize regular expression with pattern \(pattern): \(error)")
do {
return try NSRegularExpression(pattern: pattern, options: .AnchorsMatchLines)
} catch let error {
fatalError("Error constructing regular expression: \(error)")
}
}

internal func enumerateMatches(regex: NSRegularExpression, string: String, block: NSTextCheckingResult -> Void) {
let range = NSRange(location: 0, length: (string as NSString).length)
regex.enumerateMatchesInString(string, options: nil, range: range) { (result, _, _) in
block(result)
regex.enumerateMatchesInString(string, options: NSMatchingOptions(rawValue: 0), range: range) { (result, _, _) in
if let result = result {
block(result)
}
}
}
2 changes: 1 addition & 1 deletion MarkdownTextViewTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.indragie.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down

0 comments on commit cf4ca55

Please sign in to comment.