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 migration. #1

Open
wants to merge 1 commit 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
3 changes: 2 additions & 1 deletion DNVFontPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|

s.name = "DNVFontPicker"
s.version = "0.1"
s.version = "0.2"
s.summary = "DNVFontPicker is an iOS input view intended to assign font and character attributes."

# This description is used to generate tags and improve search results.
Expand All @@ -36,5 +36,6 @@ Pod::Spec.new do |s|
s.source_files = "DNVFontPicker/DNVFontPickerView.swift"

s.requires_arc = true
s.pod_target_xcconfig = { "SWIFT_VERSION" => "4.0" }

end
20 changes: 11 additions & 9 deletions DNVFontPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,20 @@
TargetAttributes = {
ECF900901E652DEC00A566B7 = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = Y575ZV84LS;
DevelopmentTeam = CZ7T27SHMZ;
ProvisioningStyle = Automatic;
};
ECF900A41E652DEC00A566B7 = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = Y575ZV84LS;
LastSwiftMigration = 0920;
ProvisioningStyle = Automatic;
TestTargetID = ECF900901E652DEC00A566B7;
};
ECF900AF1E652DEC00A566B7 = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = Y575ZV84LS;
LastSwiftMigration = 0920;
ProvisioningStyle = Automatic;
TestTargetID = ECF900901E652DEC00A566B7;
};
Expand Down Expand Up @@ -423,25 +425,25 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = Y575ZV84LS;
DEVELOPMENT_TEAM = CZ7T27SHMZ;
INFOPLIST_FILE = DNVFontPicker/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = DnV1eX.DNVFontPicker;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
ECF900BB1E652DEC00A566B7 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = Y575ZV84LS;
DEVELOPMENT_TEAM = CZ7T27SHMZ;
INFOPLIST_FILE = DNVFontPicker/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = DnV1eX.DNVFontPicker;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -455,7 +457,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = DnV1eX.DNVFontPickerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DNVFontPicker.app/DNVFontPicker";
};
name = Debug;
Expand All @@ -470,7 +472,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = DnV1eX.DNVFontPickerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DNVFontPicker.app/DNVFontPicker";
};
name = Release;
Expand All @@ -484,7 +486,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = DnV1eX.DNVFontPickerUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_TARGET_NAME = DNVFontPicker;
};
name = Debug;
Expand All @@ -498,7 +500,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = DnV1eX.DNVFontPickerUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_TARGET_NAME = DNVFontPicker;
};
name = Release;
Expand Down
74 changes: 37 additions & 37 deletions DNVFontPicker/DNVFontPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public extension UIFont {

func withFamilyName(_ fontFamilyName: String) -> UIFont {

if let fontDescriptor = fontDescriptor.withFamily(fontFamilyName).matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptorFamilyAttribute]).first {
if let fontDescriptor = fontDescriptor.withFamily(fontFamilyName).matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptor.AttributeName.family]).first {
return UIFont(descriptor: fontDescriptor, size: pointSize)
}
else {
Expand All @@ -63,12 +63,12 @@ public extension UIFont {
}


public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
@objc public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDelegate {

public let pickerView = UIPickerView()
@objc public let pickerView = UIPickerView()


public var toolbar: UIToolbar {
@objc public var toolbar: UIToolbar {
let toolbar = UIToolbar()
toolbar.bounds.size.height = 44

Expand All @@ -82,7 +82,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}


public weak var textView: UITextView? {
@objc public weak var textView: UITextView? {
didSet {
guard let textView = textView else {
return
Expand All @@ -99,32 +99,32 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele

switch component {
case PickerViewComponent.fontFamily, PickerViewComponent.fontSize, ToolbarComponent.isBold, ToolbarComponent.isItalic:
attributedString.enumerateAttribute(NSFontAttributeName, in: selectedRange) { attribute, range, _ in
attributedString.enumerateAttribute(NSAttributedStringKey.font, in: selectedRange) { attribute, range, _ in

let font = (attribute as! UIFont).withPickerComponent(component)
attributedString.addAttribute(NSFontAttributeName, value: font, range: range)
attributedString.addAttribute(NSAttributedStringKey.font, value: font, range: range)
}
let font = (textView.typingAttributes[NSFontAttributeName] as! UIFont).withPickerComponent(component)
textView.typingAttributes[NSFontAttributeName] = font
let font = (textView.typingAttributes[NSAttributedStringKey.font.rawValue] as! UIFont).withPickerComponent(component)
textView.typingAttributes[NSAttributedStringKey.font.rawValue] = font

case let PickerViewComponent.fontColor(color):
attributedString.addAttribute(NSForegroundColorAttributeName, value: color, range: selectedRange)
textView.typingAttributes[NSForegroundColorAttributeName] = color
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: selectedRange)
textView.typingAttributes[NSAttributedStringKey.foregroundColor.rawValue] = color

case let ToolbarComponent.isUnderlined(underlined):
let underlineStyle: NSUnderlineStyle = (underlined ? .styleSingle : .styleNone)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: underlineStyle, range: selectedRange)
textView.typingAttributes[NSUnderlineStyleAttributeName] = underlineStyle
attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: underlineStyle, range: selectedRange)
textView.typingAttributes[NSAttributedStringKey.underlineStyle.rawValue] = underlineStyle

case let ToolbarComponent.isStrikethrough(strikethrough):
let strikethroughStyle: NSUnderlineStyle = (strikethrough ? .styleSingle : .styleNone)
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: strikethroughStyle, range: selectedRange)
textView.typingAttributes[NSStrikethroughStyleAttributeName] = strikethroughStyle
attributedString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: strikethroughStyle, range: selectedRange)
textView.typingAttributes[NSAttributedStringKey.strikethroughStyle.rawValue] = strikethroughStyle

case let ToolbarComponent.isHighlighted(highlighted):
let backgroundColor: UIColor? = (highlighted ? .yellow : nil)
attributedString.addAttribute(NSBackgroundColorAttributeName, value: backgroundColor as Any, range: selectedRange)
textView.typingAttributes[NSBackgroundColorAttributeName] = backgroundColor
attributedString.addAttribute(NSAttributedStringKey.backgroundColor, value: backgroundColor as Any, range: selectedRange)
textView.typingAttributes[NSAttributedStringKey.backgroundColor.rawValue] = backgroundColor

default:
break
Expand Down Expand Up @@ -173,7 +173,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}


public var fontFamilies: [String] = [UIFont.systemFontFamilyName] + UIFont.familyNames.sorted() {
@objc public var fontFamilies: [String] = [UIFont.systemFontFamilyName] + UIFont.familyNames.sorted() {
didSet {
if !fontFamilies.contains(UIFont.systemFontFamilyName) {
fontFamilies.insert(UIFont.systemFontFamilyName, at: 0)
Expand All @@ -182,21 +182,21 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}
}

public var fontSizes: [CGFloat]? = [7, 8, 9, 10, 11, 12, 14, 16, 18, 24, 36, 48, 64] {
@objc public var fontSizes: [CGFloat]? = [7, 8, 9, 10, 11, 12, 14, 16, 18, 24, 36, 48, 64] {
didSet {
fontSizes?.sort()
pickerView.reloadAllComponents()
}
}

public var fontColors: [UIColor]? = DNVFontPickerView.colors(count: 16) {
@objc public var fontColors: [UIColor]? = DNVFontPickerView.colors(count: 16) {
didSet {
pickerView.reloadAllComponents()
}
}


public static func colors(count: Int) -> [UIColor]? {
@objc public static func colors(count: Int) -> [UIColor]? {

if count < 1 {
return nil
Expand All @@ -223,7 +223,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}


override init(frame: CGRect) {
@objc override init(frame: CGRect) {

super.init(frame: frame)

Expand All @@ -237,14 +237,14 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
addSubview(pickerView)
}

required public init?(coder aDecoder: NSCoder) {
@objc required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


public func update(attributes: [String: Any]) {
@objc public func update(attributes: [String: Any]) {

let font = attributes[NSFontAttributeName] as? UIFont ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
let font = attributes[NSAttributedStringKey.font.rawValue] as? UIFont ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
pickerView.selectRow(fontFamilies.index(of: font.familyName) ?? 0, inComponent: 0, animated: false)

if let fontSizes = fontSizes {
Expand All @@ -255,7 +255,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}

if let fontColors = fontColors {
let color = attributes[NSForegroundColorAttributeName] as? UIColor ?? .black
let color = attributes[NSAttributedStringKey.foregroundColor.rawValue] as? UIColor ?? .black
if fontColors.index(of: color) == nil {
self.fontColors!.append(color)
}
Expand All @@ -266,18 +266,18 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele

public var onUpdate: ((_ component: PickerComponent) -> ())?

public var onUpdate_objc: ((_ attributeName: String, _ value: Any) -> ())?
@objc public var onUpdate_objc: ((_ attributeName: String, _ value: Any) -> ())?


// MARK: - UIPickerViewDataSource

public func numberOfComponents(in pickerView: UIPickerView) -> Int {
@objc public func numberOfComponents(in pickerView: UIPickerView) -> Int {

return PickerViewComponent.count(forPicker: self)
}


public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
@objc public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

switch PickerViewComponent(component, forPicker: self)! {
case .fontFamily:
Expand All @@ -294,7 +294,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele

// MARK: - UIPickerViewDelegate

public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
@objc public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

if let view = view {
return view
Expand Down Expand Up @@ -323,7 +323,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}


public func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
@objc public func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {

switch PickerViewComponent(component, forPicker: self)! {
case .fontFamily:
Expand All @@ -338,7 +338,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}


public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
@objc public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

let pickerViewComponent = PickerViewComponent(component, forPicker: self, selectedRow: row)!

Expand All @@ -347,19 +347,19 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
if let onUpdate = onUpdate_objc {
switch pickerViewComponent {
case let .fontFamily(family):
onUpdate(UIFontDescriptorFamilyAttribute, family)
onUpdate(UIFontDescriptor.AttributeName.family.rawValue, family)
case let .fontSize(size):
onUpdate(UIFontDescriptorSizeAttribute, size as NSNumber)
onUpdate(UIFontDescriptor.AttributeName.size.rawValue, size as NSNumber)
case let .fontColor(color):
onUpdate(NSForegroundColorAttributeName, color)
onUpdate(NSAttributedStringKey.foregroundColor.rawValue, color)
}
}
}


// MARK: - UIBarButtonItem Actions

func switchKeyboard(sender: UIBarButtonItem) {
@objc func switchKeyboard(sender: UIBarButtonItem) {

if textView?.inputView == nil {
textView?.inputView = self
Expand All @@ -372,7 +372,7 @@ public class DNVFontPickerView: UIView, UIPickerViewDataSource, UIPickerViewDele
}


func hideKeyboard(sender: UIBarButtonItem) {
@objc func hideKeyboard(sender: UIBarButtonItem) {

textView?.inputView = nil
textView?.resignFirstResponder()
Expand Down
2 changes: 1 addition & 1 deletion DNVFontPicker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ViewController: UIViewController, UITextViewDelegate {
}


func keyboardWillChangeFrame(notification: Notification) {
@objc func keyboardWillChangeFrame(notification: Notification) {

guard let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let animationDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue,
Expand Down
1 change: 1 addition & 0 deletions DNVFontPickerUITests/DNVFontPickerUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import XCTest

@available(iOS 9.0, *)
class DNVFontPickerUITests: XCTestCase {

override func setUp() {
Expand Down