diff --git a/FittableFontLabel/.swiftlint.yml b/FittableFontLabel/.swiftlint.yml index 79dedf9..9c5351b 100644 --- a/FittableFontLabel/.swiftlint.yml +++ b/FittableFontLabel/.swiftlint.yml @@ -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 \ No newline at end of file diff --git a/FittableFontLabel/Source/FittableFontLabel.swift b/FittableFontLabel/Source/FittableFontLabel.swift index 31c7721..a51c2b9 100644 --- a/FittableFontLabel/Source/FittableFontLabel.swift +++ b/FittableFontLabel/Source/FittableFontLabel.swift @@ -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 @@ -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) } diff --git a/FittableFontLabel/Source/UILabelExtension.swift b/FittableFontLabel/Source/UILabelExtension.swift index 17d72b2..ba43078 100644 --- a/FittableFontLabel/Source/UILabelExtension.swift +++ b/FittableFontLabel/Source/UILabelExtension.swift @@ -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 } @@ -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 @@ -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) @@ -101,7 +101,7 @@ private extension UILabel { let diff = maxSize - minSize guard diff > 0.1 else { switch state { - case .TooSmall: + case .tooSmall: return maxSize default: return minSize @@ -109,33 +109,33 @@ private extension UILabel { } 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 } } diff --git a/Source/FittableFontLabel.swift b/Source/FittableFontLabel.swift index 31c7721..1c74911 100644 --- a/Source/FittableFontLabel.swift +++ b/Source/FittableFontLabel.swift @@ -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 @@ -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)) } diff --git a/Source/UILabelExtension.swift b/Source/UILabelExtension.swift index aa09816..2f75755 100644 --- a/Source/UILabelExtension.swift +++ b/Source/UILabelExtension.swift @@ -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 }