-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
4,752 additions
and
2,648 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
Aztec/Classes/Extensions/NSAttributedString+Analyzers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
|
||
// MARK: - NSAttributedString Analyzer Helpers | ||
// | ||
extension NSAttributedString { | ||
|
||
/// Returns true if the text preceding a given location contains the NSLinkAttribute. | ||
/// | ||
func isLocationPreceededByLink(_ location: Int) -> Bool { | ||
let beforeRange = NSRange(location: location - 1, length: 1) | ||
guard beforeRange.location >= 0 else { | ||
return false | ||
} | ||
|
||
return attribute(NSLinkAttributeName, at: beforeRange.location, effectiveRange: nil) != nil | ||
} | ||
|
||
/// Returns true if the text immediately succeding a given location contains the NSLinkAttribute. | ||
/// | ||
func isLocationSuccededByLink(_ location: Int) -> Bool { | ||
let afterRange = NSRange(location: location, length: 1) | ||
guard afterRange.endLocation < length else { | ||
return false | ||
} | ||
|
||
return attribute(NSLinkAttributeName, at: afterRange.location, effectiveRange: nil) != nil | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Aztec/Classes/Extensions/NSAttributedString+AttributeDifferences.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
extension NSAttributedString { | ||
|
||
typealias ProcessDifferenceClosure = (NSRange, String, Any?, Any?) -> () | ||
|
||
func enumerateAttributeDifferences( | ||
in range: NSRange, | ||
against targetAttributes: [String : Any], | ||
do processDifference: ProcessDifferenceClosure) { | ||
|
||
enumerateAttributes(in: range, options: [], using: { (attributes, subRange, stop) in | ||
NSAttributedString.enumerateAttributeDifferences( | ||
in: subRange, | ||
sourceAttributes: attributes, | ||
targetAttributes: targetAttributes, | ||
do: processDifference) | ||
}) | ||
} | ||
|
||
static func enumerateAttributeDifferences( | ||
in range: NSRange, | ||
sourceAttributes: [String : Any], | ||
targetAttributes: [String : Any], | ||
do processDifference: ProcessDifferenceClosure) { | ||
|
||
let sourceKeys = Set(sourceAttributes.keys) | ||
let targetKeys = Set(targetAttributes.keys) | ||
let joinedKeys = sourceKeys.union(targetKeys) | ||
|
||
for key in joinedKeys { | ||
let sourceValue = sourceAttributes.contains(where: { return $0.key == key }) ? sourceAttributes[key] : nil | ||
let targetValue = targetAttributes.contains(where: { return $0.key == key }) ? targetAttributes[key] : nil | ||
|
||
processDifference(range, key, sourceValue, targetValue) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,52 @@ | ||
import Foundation | ||
|
||
|
||
// MARK: - String Extensions | ||
// MARK: - String NSRange and Location convertion Extensions | ||
// | ||
extension String | ||
{ | ||
func rangeFromNSRange(_ nsRange : NSRange) -> Range<String.Index>? { | ||
|
||
let rangeStartIndex = utf16.startIndex.advanced(by: nsRange.location) | ||
let rangeEndIndex = rangeStartIndex.advanced(by: nsRange.length) | ||
|
||
if let from = String.Index(rangeStartIndex, within: self), | ||
let to = String.Index(rangeEndIndex, within: self) { | ||
return from ..< to | ||
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location) | ||
let to16 = utf16.index(from16, offsetBy: nsRange.length) | ||
|
||
guard | ||
let from = from16.samePosition(in: self), | ||
let to = to16.samePosition(in: self) | ||
else { return nil } | ||
return from ..< to | ||
|
||
} | ||
|
||
func indexFromLocation(_ location: Int) -> String.Index? { | ||
guard | ||
let from16 = utf16.index(utf16.startIndex, offsetBy: location, limitedBy: utf16.endIndex), | ||
let from = from16.samePosition(in: self) | ||
else { return nil } | ||
return from | ||
} | ||
|
||
func isLastValidLocation(_ location: Int) -> Bool { | ||
if self.isEmpty { | ||
return false | ||
} | ||
return index(before: endIndex) == indexFromLocation(location) | ||
} | ||
|
||
func location(after: Int) -> Int? { | ||
guard let currentIndex = indexFromLocation(after) else { | ||
return nil | ||
} | ||
let afterIndex = index(after: currentIndex) | ||
let after16 = afterIndex.samePosition(in: utf16) | ||
return utf16.distance(from: utf16.startIndex, to: after16) | ||
} | ||
|
||
func location(before: Int) -> Int? { | ||
guard let currentIndex = indexFromLocation(before) else { | ||
return nil | ||
} | ||
|
||
return nil | ||
let beforeIndex = index(before: currentIndex) | ||
let before16 = beforeIndex.samePosition(in: utf16) | ||
return utf16.distance(from: utf16.startIndex, to: before16) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
|
||
// MARK: - UIFont Emoji Helpers | ||
// | ||
extension UIFont { | ||
|
||
/// Indicates if the current font instance matches with iOS's Internal Emoji Font, or not. | ||
/// | ||
var isAppleEmojiFont: Bool { | ||
return fontName == ".AppleColorEmojiUI" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.