Skip to content

Commit

Permalink
Migrate to Swift 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
inkyfox committed Oct 19, 2016
1 parent 94adf26 commit 34fe28c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 61 deletions.
17 changes: 11 additions & 6 deletions KRWordWrapLabel.podspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Pod::Spec.new do |s|
s.name = "KRWordWrapLabel"
s.version = "1.0.0"
s.version = "2.0.0"
s.summary = "UILabel subclass which provides **Word Wrap** based on white spaces not depending on languages"
s.description = <<-DESC
# KRWordWrapLabel
![Swift](https://img.shields.io/badge/Swift-2.3-orange.svg)
``UILabel`` subclass which provides **Word Wrap** based on white spaces not depending on languages
Expand All @@ -30,17 +29,19 @@ UILabel은 Word Wrap 모드를 지원하지만 한글의 경우 글자 단위로
### CocoaPods
#### Swift 3
```Ruby
pod 'KRWordWrapLabel'
```
## Requirements
- Swift 2.3
#### Swift 2.3
```Ruby
pod 'KRWordWrapLabel' => '~>1'
```
## Author
Yongha Yoo, inkyfox@oo-v.com
Yongha Yoo, http://inkyfox.oo-v.com
## License
Expand All @@ -57,4 +58,8 @@ MIT

s.source_files = 'Sources/KRWordWrapLabel.swift'

s.pod_target_xcconfig = {
'SWIFT_VERSION' => '3.0'
}

end
4 changes: 2 additions & 2 deletions KRWordWrapLabelDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "KRWordWrapLabelDemo/KRWordWrapLabelDemo-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -380,7 +380,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "KRWordWrapLabelDemo/KRWordWrapLabelDemo-Bridging-Header.h";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
12 changes: 6 additions & 6 deletions KRWordWrapLabelDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
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) {
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) {
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) {
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) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
12 changes: 6 additions & 6 deletions KRWordWrapLabelDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ class ViewController: UIViewController {
updateLabel()
}

override func viewDidAppear(animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
labelWidthConstraint.constant = min(CGFloat(widthStepper.maximumValue), (self.view.window?.frame.width ?? 320) - 48)
widthStepper.value = Double(labelWidthConstraint.constant)
widthLabel.text = "\(widthStepper.value)"
}

private func updateLabel() {
fileprivate func updateLabel() {
let index = alignmentSegment.selectedSegmentIndex
label.textAlignment = index == 1 ? .Center : index == 2 ? .Right: .Left
label.textAlignment = index == 1 ? .center : index == 2 ? .right: .left
label.numberOfLines = Int(maxLinesStepper.value)
label.font = label.font.fontWithSize(CGFloat(fontSizeStepper.value))
label.font = label.font.withSize(CGFloat(fontSizeStepper.value))
label.minimumScaleFactor = CGFloat(minFontScaleStepper.value)

maxLinesLabel.text = "\(label.numberOfLines)"
Expand All @@ -51,12 +51,12 @@ class ViewController: UIViewController {

}

@IBAction func updateLabel(sender: AnyObject) {
@IBAction func updateLabel(_ sender: AnyObject) {
updateLabel()
}


@IBAction func updateWidth(sender: AnyObject) {
@IBAction func updateWidth(_ sender: AnyObject) {
labelWidthConstraint.constant = CGFloat(widthStepper.value)
widthLabel.text = "\(widthStepper.value)"
}
Expand Down
82 changes: 41 additions & 41 deletions Sources/KRWordWrapLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@

import UIKit

@IBDesignable public class KRWordWrapLabel: UILabel {
@IBDesignable open class KRWordWrapLabel: UILabel {

@IBInspectable public var ellipsis: String = "..." { didSet { self.updateWords() } }
@IBInspectable open var ellipsis: String = "..." { didSet { self.updateWords() } }

override public var text: String? { didSet { self.updateWords() } }
override public var font: UIFont! { didSet { self.updateWords() } }
override public var textColor: UIColor! { didSet { self.updateWords() } }
override open var text: String? { didSet { self.updateWords() } }
override open var font: UIFont! { didSet { self.updateWords() } }
override open var textColor: UIColor! { didSet { self.updateWords() } }

@IBInspectable public var lineSpace: CGFloat = 0 { didSet { self.updateWordLayout() } }
@IBInspectable open var lineSpace: CGFloat = 0 { didSet { self.updateWordLayout() } }

override public var bounds: CGRect { didSet { self.updateWordLayout() } }
override public var numberOfLines: Int { didSet { self.updateWordLayout() } }
override public var textAlignment: NSTextAlignment { didSet { self.updateWordLayout() } }
override open var bounds: CGRect { didSet { self.updateWordLayout() } }
override open var numberOfLines: Int { didSet { self.updateWordLayout() } }
override open var textAlignment: NSTextAlignment { didSet { self.updateWordLayout() } }

private var intrinsicSize: CGSize = CGSizeZero
fileprivate var intrinsicSize: CGSize = CGSize.zero

override public func awakeFromNib() {
override open func awakeFromNib() {
super.awakeFromNib()
self.updateWords()
}

override public func intrinsicContentSize() -> CGSize {
if self.lineBreakMode == .ByWordWrapping && self.doWordWrap {
override open var intrinsicContentSize : CGSize {
if self.lineBreakMode == .byWordWrapping && self.doWordWrap {
return intrinsicSize
} else {
return super.intrinsicContentSize()
return super.intrinsicContentSize
}
}

// MARK: - Codes for Word Wrap

private class Word {
fileprivate class Word {
let text: NSAttributedString
let size: CGSize
let precedingSpace: CGFloat
var origin: CGPoint = CGPointZero
var origin: CGPoint = CGPoint.zero
var visible: Bool = true

init(text: NSAttributedString, size: CGSize, precedingSpaceWidth: CGFloat) {
Expand All @@ -53,53 +53,53 @@ import UIKit
}
}

private var ellipsisWord: Word!
private var paragraphs: [[Word]]?
private var doWordWrap = true
fileprivate var ellipsisWord: Word!
fileprivate var paragraphs: [[Word]]?
fileprivate var doWordWrap = true

override public func drawRect(rect: CGRect) {
if self.lineBreakMode == .ByWordWrapping && self.doWordWrap {
override open func draw(_ rect: CGRect) {
if self.lineBreakMode == .byWordWrapping && self.doWordWrap {
guard let paragraphs = self.paragraphs else { return }

if self.ellipsisWord.visible {
self.ellipsisWord.text.drawAtPoint(self.ellipsisWord.origin)
self.ellipsisWord.text.draw(at: self.ellipsisWord.origin)
}

for words in paragraphs {
for word in words {
if !word.visible {
return
}
word.text.drawAtPoint(word.origin)
word.text.draw(at: word.origin)
}
}


} else {
super.drawRect(rect)
super.draw(rect)
}
}

private func updateWords() {
fileprivate func updateWords() {
let maxFontSize = self.font.pointSize
let minFontSize = self.adjustsFontSizeToFitWidth || self.minimumScaleFactor > 0 ? maxFontSize * self.minimumScaleFactor : maxFontSize
for size in maxFontSize.stride(through: minFontSize, by: -0.5) {
for size in stride(from: maxFontSize, through: minFontSize, by: -0.5) {
if updateWords(size) {
return
}
}
}

private func updateWords(fontSize: CGFloat) -> Bool {
fileprivate func updateWords(_ fontSize: CGFloat) -> Bool {
guard let text = self.text else { return true }

let font = self.font.fontWithSize(fontSize)
let font = self.font.withSize(fontSize)
var w = 0
self.paragraphs = text.characters
.split(allowEmptySlices: true) { (c: Character) -> Bool in return c == "\n" || c == "\r\n" }
.split(omittingEmptySubsequences: false) { (c: Character) -> Bool in return c == "\n" || c == "\r\n" }
.map(String.init)
.map { (paragraph: String) -> [KRWordWrapLabel.Word] in
return paragraph.characters.split(" ", allowEmptySlices: true)
return paragraph.characters.split(separator: " ", omittingEmptySubsequences: false)
.map(String.init)
.map { s -> NSAttributedString? in
return s == "" ? nil :
Expand All @@ -109,11 +109,11 @@ import UIKit
.flatMap { t -> Word? in
if let text = t {
let size = text.size()
let spaceWidth = NSAttributedString(string: String(count: w, repeatedValue: Character(" ")), attributes: [NSFontAttributeName : font]).size().width
let spaceWidth = NSAttributedString(string: String(repeating: " ", count: w), attributes: [NSFontAttributeName : font]).size().width
w = 1
return Word(
text: text,
size: CGSizeMake(ceil(size.width), ceil(size.height)),
size: CGSize(width: ceil(size.width), height: ceil(size.height)),
precedingSpaceWidth: ceil(spaceWidth))
} else {
w += 1
Expand All @@ -129,7 +129,7 @@ import UIKit
let size = text.size()
return [Word(
text: text,
size: CGSizeMake(ceil(size.width), ceil(size.height)),
size: CGSize(width: ceil(size.width), height: ceil(size.height)),
precedingSpaceWidth: 0)]
}
}
Expand All @@ -142,34 +142,34 @@ import UIKit

self.ellipsisWord = Word(
text: text,
size: CGSizeMake(ceil(size.width), ceil(size.height)),
size: CGSize(width: ceil(size.width), height: ceil(size.height)),
precedingSpaceWidth: 0)
}

return self.updateWordLayout()
}

private func updateWordLayout() -> Bool {
fileprivate func updateWordLayout() -> Bool {
guard let paragraphs = self.paragraphs else { return true }

self.doWordWrap = true

let width = self.bounds.width

var totalSize:CGSize = CGSizeZero
var rowSize: CGSize = CGSizeZero
var totalSize:CGSize = CGSize.zero
var rowSize: CGSize = CGSize.zero

var rowCount = 1
var colCount = 0

var colWords: [Word] = []

func newRow() {
var x = self.textAlignment == .Center ? (width - rowSize.width) / 2 : self.textAlignment == .Right ? width - rowSize.width : 0
var x = self.textAlignment == .center ? (width - rowSize.width) / 2 : self.textAlignment == .right ? width - rowSize.width : 0

let y = totalSize.height + rowSize.height

for (index, word) in colWords.enumerate() {
for (index, word) in colWords.enumerated() {
if index > 0 {
x += word.precedingSpace
}
Expand All @@ -183,7 +183,7 @@ import UIKit
totalSize.height += rowSize.height

colWords.removeAll()
rowSize = CGSizeZero
rowSize = CGSize.zero
colCount = 0
rowCount += 1
}
Expand All @@ -195,7 +195,7 @@ import UIKit
words[0].visible = false
}

loop: for (index, words) in paragraphs.enumerate() {
loop: for (index, words) in paragraphs.enumerated() {
for word in words {
var x = rowSize.width
if word.size.width > width {
Expand Down

0 comments on commit 34fe28c

Please sign in to comment.