Skip to content

Commit

Permalink
Merge pull request #26 from tbaranes/feature/swift4_cleanup
Browse files Browse the repository at this point in the history
Swift 4 leftover
  • Loading branch information
Tom Baranes authored Nov 6, 2017
2 parents f9162fd + 6979440 commit 981b708
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
28 changes: 28 additions & 0 deletions FittableFontLabel/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
line_length: 180
opt_in_rules:
- closure_spacing
- conditional_returns_on_newline
- empty_count
- explicit_init
- overridden_super_call
- redundant_nil_coalescing
- nimble_operator
- closure_end_indentation
- first_where
- attributes
- operator_usage_whitespace
- prohibited_super_call
- number_separator
- object_literal
- fatal_error_message
- private_outlet
- implicit_return
- multiline_parameters
- vertical_parameter_alignment_on_call
- unneeded_parentheses_in_closure_argument
- joined_default_parameter
- pattern_matching_keywords
- array_init
- contains_over_first_not_nil
- let_var_whitespace
- literal_expression_end_indentation
# - trailing_closure // false positive
included:
- ../Source
7 changes: 4 additions & 3 deletions FittableFontLabel/Source/FittableFontLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import UIKit

// An UILabel subclass allowing you to automatize the process of adjusting the font size.
@IBDesignable open class FittableFontLabel: UILabel {
@IBDesignable
open class FittableFontLabel: UILabel {

// MARK: Properties

Expand Down Expand Up @@ -94,9 +95,9 @@ import UIKit

// MARK: Helpers

fileprivate extension FittableFontLabel {
extension FittableFontLabel {

func adjustFontSize() {
private func adjustFontSize() {
if autoAdjustFontSize {
fontSizeToFit(maxFontSize: maxFontSize, minFontScale: minFontScale)
}
Expand Down
38 changes: 19 additions & 19 deletions FittableFontLabel/Source/UILabelExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public extension UILabel {
let minFontScale = minFontScale.isNaN ? 0.1 : minFontScale
let minimumFontSize = maxFontSize * minFontScale
let rectSize = rectSize ?? bounds.size
guard string.count != 0 else {
guard !string.isEmpty else {
return self.font.pointSize
}

Expand All @@ -68,9 +68,9 @@ public extension UILabel {

// MARK: - Helpers

private extension UILabel {
extension UILabel {

func currentAttributedStringAttributes() -> [NSAttributedStringKey: Any] {
private func currentAttributedStringAttributes() -> [NSAttributedStringKey: Any] {
var newAttributes = [NSAttributedStringKey: Any]()
attributedText?.enumerateAttributes(in: NSRange(0..<(text?.count ?? 0)), options: .longestEffectiveRangeNotRequired, using: { attributes, range, stop in
newAttributes = attributes
Expand All @@ -82,13 +82,13 @@ private extension UILabel {

// MARK: - Search

private extension UILabel {
extension UILabel {

enum FontSizeState {
case Fit, TooBig, TooSmall
private enum FontSizeState {
case fit, tooBig, tooSmall
}

func binarySearch(string: String, minSize: CGFloat, maxSize: CGFloat, size: CGSize, constraintSize: CGSize) -> CGFloat {
private func binarySearch(string: String, minSize: CGFloat, maxSize: CGFloat, size: CGSize, constraintSize: CGSize) -> CGFloat {
let fontSize = (minSize + maxSize) / 2
var attributes = currentAttributedStringAttributes()
attributes[NSAttributedStringKey.font] = font.withSize(fontSize)
Expand All @@ -101,41 +101,41 @@ private extension UILabel {
let diff = maxSize - minSize
guard diff > 0.1 else {
switch state {
case .TooSmall:
case .tooSmall:
return maxSize
default:
return minSize
}
}

switch state {
case .Fit: return fontSize
case .TooBig: return binarySearch(string: string, minSize: minSize, maxSize: fontSize, size: size, constraintSize: constraintSize)
case .TooSmall: return binarySearch(string: string, minSize: fontSize, maxSize: maxSize, size: size, constraintSize: constraintSize)
case .fit: return fontSize
case .tooBig: return binarySearch(string: string, minSize: minSize, maxSize: fontSize, size: size, constraintSize: constraintSize)
case .tooSmall: return binarySearch(string: string, minSize: fontSize, maxSize: maxSize, size: size, constraintSize: constraintSize)
}
}

func singleLineSizeState(rect: CGRect, size: CGSize) -> FontSizeState {
private func singleLineSizeState(rect: CGRect, size: CGSize) -> FontSizeState {
if rect.width >= size.width + 10 && rect.width <= size.width {
return .Fit
return .fit
} else if rect.width > size.width {
return .TooBig
return .tooBig
} else {
return .TooSmall
return .tooSmall
}
}

func multiLineSizeState(rect: CGRect, size: CGSize) -> FontSizeState {
private func multiLineSizeState(rect: CGRect, size: CGSize) -> FontSizeState {
// if rect within 10 of size
if rect.height < size.height + 10 &&
rect.height > size.height - 10 &&
rect.width > size.width + 10 &&
rect.width < size.width - 10 {
return .Fit
return .fit
} else if rect.height > size.height || rect.width > size.width {
return .TooBig
return .tooBig
} else {
return .TooSmall
return .tooSmall
}
}

Expand Down
5 changes: 3 additions & 2 deletions Source/FittableFontLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import UIKit

// An UILabel subclass allowing you to automatize the process of adjusting the font size.
@IBDesignable open class FittableFontLabel: UILabel {
@IBDesignable
open class FittableFontLabel: UILabel {

// MARK: Properties

Expand Down Expand Up @@ -86,7 +87,7 @@ import UIKit
// MARK: Insets

open override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}

Expand Down
2 changes: 1 addition & 1 deletion Source/UILabelExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public extension UILabel {
let minFontScale = minFontScale.isNaN ? 0.1 : minFontScale
let minimumFontSize = maxFontSize * minFontScale
let rectSize = rectSize ?? bounds.size
guard string.characters.count != 0 else {
guard !string.isEmpty else {
return self.font.pointSize
}

Expand Down

0 comments on commit 981b708

Please sign in to comment.