Skip to content

V1 #6

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

Merged
merged 2 commits into from
Oct 18, 2017
Merged

V1 #6

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
2 changes: 1 addition & 1 deletion Examples/SelectableTestView-Example/UIClassValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public struct UIClassValidator: ContainerTextSelectionValidator {
public let validator: TextSelectionValidator = PrefixValidator(prefix: "UI", caseSensitive: true)

public var selectionAttributes: [String : Any]? = [
NSForegroundColorAttributeName: UIColor.orange
NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange
]
}
2 changes: 1 addition & 1 deletion Examples/SelectableTestView-Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ViewController: UIViewController, SelectableTextViewDelegate {

@IBAction func addExpansionButtonToggled(_ sender: UISwitch) {
if sender.isOn {
let attributes: [String: Any] = [HighlightedTextSelectionAttributes.SelectedBackgroundColorAttribute : UIColor.purple.withAlphaComponent(0.5)]
let attributes: [NSAttributedStringKey: Any] = [HighlightedTextSelectionAttributes.SelectedBackgroundColorAttribute : UIColor.purple.withAlphaComponent(0.5)]
let collapsedNumberOfLines = max(Int(numberOfLinesStepper.value), 1)
textView.addExpansionButton(collapsedState: ("More...", collapsedNumberOfLines),
expandedState: ("Less", 0),
Expand Down
33 changes: 25 additions & 8 deletions SelectableTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,19 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0820;
LastUpgradeCheck = 0820;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = jhurray;
TargetAttributes = {
26577D981E46BDD1008DB9AB = {
CreatedOnToolsVersion = 8.2;
DevelopmentTeam = 43LEM8BQ2H;
LastSwiftMigration = 0820;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
26577DA11E46BDD1008DB9AB = {
CreatedOnToolsVersion = 8.2;
DevelopmentTeam = 43LEM8BQ2H;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
26EC5D6B1E47A82900BA66D9 = {
Expand Down Expand Up @@ -442,15 +443,21 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -495,15 +502,21 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -549,7 +562,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -569,7 +583,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.guaranteed.SelectableTextView;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -582,7 +597,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.guaranteed.SelectableTextViewTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -595,7 +611,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.guaranteed.SelectableTextViewTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -609,7 +626,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.guaranteed.SelectableTestView-Example-iOS";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -623,7 +640,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.guaranteed.SelectableTestView-Example-iOS";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
6 changes: 3 additions & 3 deletions Source/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ internal extension String {

func truncate(leadingCharacters: Int) -> String {
let range = Range(uncheckedBounds: (startIndex, index(startIndex, offsetBy: leadingCharacters)))
let start: String = substring(with: range)
let start: String = String(self[range])
let truncation: String = "..."
return start.appending(truncation)
}

func width(withAttributes attributes: [String: Any]?) -> CGFloat {
func width(withAttributes attributes: [NSAttributedStringKey: Any]?) -> CGFloat {
let attributedString = NSAttributedString(string: self, attributes: attributes)
return attributedString.size().width
}

func truncatedString(fittingWidth width: CGFloat, attributes: [String: Any]?) -> String? {
func truncatedString(fittingWidth width: CGFloat, attributes: [NSAttributedStringKey: Any]?) -> String? {

func passesTest(leadingCharacters: Int) -> Bool {
let truncatedString = truncate(leadingCharacters: leadingCharacters)
Expand Down
30 changes: 15 additions & 15 deletions Source/SelectableTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public final class SelectableTextView : UIView {
setNeedsLayout()
}
}
public var selectionAttributes: [String:AnyObject]? {
public var selectionAttributes: [NSAttributedStringKey: AnyObject]? {
didSet {
reloadData()
}
Expand Down Expand Up @@ -172,28 +172,28 @@ public final class SelectableTextView : UIView {
fileprivate var validators: [TextSelectionValidator] = []
fileprivate var expansionButtonModel: TextExpansionButtonModel? = nil
fileprivate var ContentSizeObservationContext: UnsafeMutableRawPointer? = nil
fileprivate var defaultAttributes: [String: Any] {
fileprivate var defaultAttributes: [NSAttributedStringKey: Any] {
let attributes = [
NSForegroundColorAttributeName:textColor,
NSFontAttributeName:font
] as [String : Any]
NSAttributedStringKey.foregroundColor.rawValue:textColor,
NSAttributedStringKey.font:font
] as! [NSAttributedStringKey : Any]
return attributes
}
fileprivate var _selectionAttributes: [String: Any] {
fileprivate var _selectionAttributes: [NSAttributedStringKey: Any] {
let defaultSelectionAttributes = [
NSForegroundColorAttributeName: tintColor,
NSFontAttributeName: UIFont.boldSystemFont(ofSize: font.pointSize + 2)
] as [String : Any]
NSAttributedStringKey.foregroundColor.rawValue: tintColor,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: font.pointSize + 2)
] as! [NSAttributedStringKey : Any]
return selectionAttributes += defaultSelectionAttributes
}
fileprivate var defaultExpansionAttributes: [String: Any] {
fileprivate var defaultExpansionAttributes: [NSAttributedStringKey: Any] {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let defaultSelectionAttributes = [
NSForegroundColorAttributeName: tintColor,
NSFontAttributeName: UIFont.boldSystemFont(ofSize: font.pointSize - 2),
NSParagraphStyleAttributeName: paragraphStyle
] as [String : Any]
NSAttributedStringKey.foregroundColor.rawValue: tintColor,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: font.pointSize - 2),
NSAttributedStringKey.paragraphStyle: paragraphStyle
] as! [NSAttributedStringKey : Any]
return selectionAttributes += defaultSelectionAttributes
}
fileprivate struct LayoutHelper {
Expand Down Expand Up @@ -258,7 +258,7 @@ public final class SelectableTextView : UIView {
}

// MARK: Public
public func addExpansionButton(collapsedState: (text: String, lines: Int), expandedState: (text: String, lines: Int), attributes: [String: Any]? = nil) {
public func addExpansionButton(collapsedState: (text: String, lines: Int), expandedState: (text: String, lines: Int), attributes: [NSAttributedStringKey: Any]? = nil) {
assert(collapsedState.lines != 0)
assert(expandedState.lines == 0 || collapsedState.lines < expandedState.lines)
let textAttributes = attributes += defaultExpansionAttributes
Expand Down
10 changes: 5 additions & 5 deletions Source/TextCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal protocol TextCellModel {
var range: Range<Int> {get}
var text: String {get}
var displayText: String? {get}
var attributes: [String:Any] {get}
var attributes: [NSAttributedStringKey: Any] {get}
var attributedText: NSAttributedString {get}
var type: TextType {get}
var highlightable: Bool {get}
Expand All @@ -39,7 +39,7 @@ internal extension TextCellModel {
internal struct Word: TextCellModel {
var range: Range<Int>
var text: String
var attributes: [String:Any]
var attributes: [NSAttributedStringKey: Any]
var displayText: String?
var highlightable: Bool

Expand All @@ -57,7 +57,7 @@ internal struct Space: TextCellModel {
private let space = " "
var range: Range<Int>
var length: Int
var attributes: [String:Any]
var attributes: [NSAttributedStringKey: Any]

var type: TextType {
return .space
Expand All @@ -74,7 +74,7 @@ internal struct Space: TextCellModel {

internal struct NewLine: TextCellModel {
var range: Range<Int>
var attributes: [String:Any]
var attributes: [NSAttributedStringKey: Any]

var type: TextType {
return .newLine
Expand All @@ -96,7 +96,7 @@ public struct TabTextModelConfig {

internal struct Tab: TextCellModel {
var range: Range<Int>
var attributes: [String:Any]
var attributes: [NSAttributedStringKey: Any]

var type: TextType {
return .tab
Expand Down
2 changes: 1 addition & 1 deletion Source/TextCellModelFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final internal class TextCellModelFactory {
break
case .word:
let substringRange = Range<_Index>(uncheckedBounds: (text.index(text.startIndex, offsetBy: currentTextChunkStart), text.index(text.startIndex, offsetBy: characterCount)))
let substring = text.substring(with: substringRange)
let substring = String(text[substringRange])
let word = Word(range: range, text: substring, attributes: attributes, displayText: nil, highlightable: false)
textModels.append(word)
break
Expand Down
2 changes: 1 addition & 1 deletion Source/TextExpansionButtonModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal struct TextExpansionButtonModel {
let collapsedText: String
let expandedNumberOfLines: Int
let collapsedNumberOfLines: Int
let attributes: [String: Any]?
let attributes: [NSAttributedStringKey: Any]?

var numberOfLines: Int {
return expanded ? expandedNumberOfLines : collapsedNumberOfLines
Expand Down
4 changes: 2 additions & 2 deletions Source/TextSelectionAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import Foundation
// MARK: TextSelectionAppearance
/// Additional attributes for text selection
public struct HighlightedTextSelectionAttributes {
public static let SelectedBackgroundColorAttribute: String = "HighlightedTextSelectionAttributes.SelectedBackgroundColorAttribute.Hurray"
public static let SelectedBackgroundColorAttribute: NSAttributedStringKey = NSAttributedStringKey(rawValue: "HighlightedTextSelectionAttributes.SelectedBackgroundColorAttribute.Hurray")
}

public protocol TextSelectionAppearance {
var selectionAttributes: [String: Any]? {get}
var selectionAttributes: [NSAttributedStringKey: Any]? {get}
var replacementText: String? {get}
}
10 changes: 5 additions & 5 deletions Source/TextSelectionValidator/LinkValidators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public extension LinkValidatorAttributes {
return true
}

public var selectionAttributes: [String : Any]? {
var attributes: [String: Any] = [:]
public var selectionAttributes: [NSAttributedStringKey : Any]? {
var attributes: [NSAttributedStringKey: Any] = [:]
if let tintColor = tintColor {
attributes[NSForegroundColorAttributeName] = tintColor
attributes[NSAttributedStringKey.foregroundColor] = tintColor
}
if underlined {
attributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.styleSingle.rawValue
attributes[NSAttributedStringKey.underlineStyle] = NSUnderlineStyle.styleSingle.rawValue
}
return attributes
}
Expand Down Expand Up @@ -60,7 +60,7 @@ public struct UnsafeLinkValidator: ContainerTextSelectionValidator, LinkValidato

private(set) public var validator: TextSelectionValidator = PrefixValidator(prefix: "http://")

private(set) public var selectionAttributes: [String : Any]? = [NSForegroundColorAttributeName: UIColor.red]
private(set) public var selectionAttributes: [String : Any]? = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]
}


Expand Down
4 changes: 2 additions & 2 deletions Source/TextSelectionValidator/TextSelectionValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import Foundation
public protocol TextSelectionValidator: TextSelectionAppearance {

var identifier: String {get}
var selectionAttributes: [String: Any]? {get}
var selectionAttributes: [NSAttributedStringKey: Any]? {get}
var replacementText: String? {get}

func validate(text: String) -> Bool
}

public extension TextSelectionValidator {

public var selectionAttributes: [String: Any]? {
public var selectionAttributes: [NSAttributedStringKey: Any]? {
return nil
}

Expand Down