From 97639e283b0fd284d7540f9770058f2c28eeb452 Mon Sep 17 00:00:00 2001 From: Dejan Skledar Date: Thu, 22 Feb 2018 12:16:54 +0100 Subject: [PATCH] Updated to Swift 4, removed some unnecessary code and cleaned the example project --- Example/Example.xcodeproj/project.pbxproj | 21 ++++++++++- Example/Example/AppDelegate.swift | 33 ----------------- .../AppIcon.appiconset/Contents.json | 15 ++++++++ Example/Example/Base.lproj/Main.storyboard | 37 ++++++++++++++++--- Example/Example/ViewController.swift | 28 +++++--------- Source/VerticalTextView.swift | 37 ++++++++++--------- 6 files changed, 94 insertions(+), 77 deletions(-) diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 6d20b98..2f7391d 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -103,7 +103,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0710; - LastUpgradeCheck = 0710; + LastUpgradeCheck = 0920; TargetAttributes = { F18E158F1C0E36830038FFD3 = { CreatedOnToolsVersion = 7.1.1; @@ -182,13 +182,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_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; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -226,13 +234,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_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; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -251,6 +267,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.1; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -263,6 +280,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.example.Example; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -274,6 +292,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.example.Example; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 2b9ca91..d2a1340 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -10,43 +10,10 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - // Override point for customization after application launch. - - let controller = ViewController() - let navigationController = UINavigationController(rootViewController: controller) - - self.window?.rootViewController = navigationController - return true } - - func applicationWillResignActive(application: UIApplication) { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. - } - - func applicationDidEnterBackground(application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - } - - func applicationWillEnterForeground(application: UIApplication) { - // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. - } - - func applicationDidBecomeActive(application: UIApplication) { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - } - - func applicationWillTerminate(application: UIApplication) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - } - - } diff --git a/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json b/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json index 118c98f..19882d5 100644 --- a/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", @@ -29,6 +39,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "3x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" } ], "info" : { diff --git a/Example/Example/Base.lproj/Main.storyboard b/Example/Example/Base.lproj/Main.storyboard index 3a2a49b..7d19fa7 100644 --- a/Example/Example/Base.lproj/Main.storyboard +++ b/Example/Example/Base.lproj/Main.storyboard @@ -1,25 +1,50 @@ - - + + + + + - + + + - + - + - + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index d5751bd..9fe5511 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -10,30 +10,20 @@ import UIKit class ViewController: UIViewController, UITextViewDelegate { - var textView : VerticalTextView! + var textView: VerticalTextView! override func viewDidLoad() { super.viewDidLoad() + title = "VerticalTextView" - self.title = "VerticalTextView" + textView = VerticalTextView(frame: view.bounds) + textView.delegate = self + textView.font = UIFont.systemFont(ofSize: 14) + textView.textColor = UIColor.black + textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fermentum massa eu pulvinar fermentum." + textView.verticalAlignment = .middle - self.textView = VerticalTextView(frame: self.view.bounds) - self.textView.delegate = self - self.textView.font = UIFont.systemFontOfSize(14) - self.textView.textColor = UIColor.blackColor() - - self.textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fermentum massa eu pulvinar fermentum." - - self.textView.verticalAlignment = .Middle - - self.view.addSubview(self.textView) - // Do any additional setup after loading the view, typically from a nib. + view.addSubview(textView) } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - } diff --git a/Source/VerticalTextView.swift b/Source/VerticalTextView.swift index 7ac5f63..7f60d40 100644 --- a/Source/VerticalTextView.swift +++ b/Source/VerticalTextView.swift @@ -23,38 +23,40 @@ import UIKit class VerticalTextView: UITextView { - enum VerticalAlignment: Int { - case Top = 0, Middle, Bottom + enum VerticalAlignment { + case top + case middle + case bottom } - var verticalAlignment: VerticalAlignment = .Middle + var verticalAlignment: VerticalAlignment = .middle - //override contentSize property and observe using didSet + // Override contentSize property and observe using didSet override var contentSize: CGSize { didSet { - let height = self.bounds.size.height - let contentHeight: CGFloat = contentSize.height + let height = bounds.size.height + let contentHeight = contentSize.height var topCorrect: CGFloat = 0.0 - switch (self.verticalAlignment) { - case .Top: - self.contentOffset = CGPointZero //set content offset to top + switch verticalAlignment { + case .top: + contentOffset = .zero //set content offset to top - case .Middle: - topCorrect = (height - contentHeight * self.zoomScale) / 2.0 + case .middle: + topCorrect = (height - contentHeight * zoomScale) / 2.0 topCorrect = topCorrect < 0 ? 0 : topCorrect - self.contentOffset = CGPoint(x: 0, y: -topCorrect) + contentOffset = CGPoint(x: 0, y: -topCorrect) - case .Bottom: + case .bottom: topCorrect = self.bounds.size.height - contentHeight topCorrect = topCorrect < 0 ? 0 : topCorrect - self.contentOffset = CGPoint(x: 0, y: -topCorrect) + contentOffset = CGPoint(x: 0, y: -topCorrect) } if contentHeight >= height { // if the contentSize is greater than the height topCorrect = contentHeight - height // set the contentOffset to be the topCorrect = topCorrect < 0 ? 0 : topCorrect // contentHeight - height of textView - self.contentOffset = CGPoint(x: 0, y: topCorrect) + contentOffset = CGPoint(x: 0, y: topCorrect) } } } @@ -63,9 +65,8 @@ class VerticalTextView: UITextView { override func layoutSubviews() { super.layoutSubviews() - let size = self.contentSize // forces didSet to be called - self.contentSize = size + contentSize = size } -} \ No newline at end of file +}