JKSwiftExtension

repository·master·Indexed 21 days ago

https://github.com/joanking/jkswiftextension

A collection of Swift extensions for Foundation, UIKit, and common programming patterns. It includes modules for FoundationExtension, UIKitExtension, Protocol, and SmallTools. Key features include dark mode support via JKDarkModeUtil, input restrictions for UITextField and UITextView (with JKPastedTextField/JKPastedTextView for paste handling), string length measurement via StringTypeLength, and extensions for UIDevice and UIApplication.

Tokens
2.2K
Snippets
10
Records
16
Agent score
75%

What's inside JKSwiftExtension

  1. Overview of JKSwiftExtension components

    master

    JKSwiftExtension is organized into several functional modules:

    • FoundationExtension: Extensions for Foundation types.
    • UIKitExtension: Extensions for UIKit types.
    • Protocol: Protocol-based utility tools.
    • SmallTools: Miscellaneous small utility tools.
  2. Install JKSwiftExtension via Swift Package Manager

    master

    To add JKSwiftExtension to your project using Swift Package Manager (SPM), follow these steps:

    1. In Xcode, go to File > Swift Packages > Add Package Dependency.
    2. Enter the repository URL: https://github.com/JoanKing/JKSwiftExtension.git.
    3. Set the dependency rule to Up to Next Major with a version starting at 2.0.0.
  3. Configure Dark Mode support with JKDarkModeUtil

    master

    JKSwiftExtension provides support for both iOS 13+ system dark mode and legacy theme support for older iOS versions.

    iOS 13+ (System Dark Mode)

    Use JKDarkModeUtil.defaultDark() after setting your window's rootViewController. You can then define colors that adapt to the user's system preference using JKDarkModeUtil.colorLightDark(light:dark:).

    Legacy Support (iOS < 13)

    Use JKThemeProvider and the JKThemeable protocol. You must register an observer to listen for mode changes.

    Best Practice: Business Colors

    It is recommended to extend UIColor with business-specific colors to keep your code clean.

    // 1. Initialize
    JKDarkModeUtil.defaultDark()
    
    // 2. Define business colors
    extension UIColor {
        private(set) static var cB1: UIColor = JKDarkModeUtil.colorLightDark(light: .green, dark: .blue)
    }
    
    // 3. Use in UI
    self.view.backgroundColor = .cB1
  4. Install JKSwiftExtension via CocoaPods

    master

    To add JKSwiftExtension to your project using CocoaPods, add the following to your Podfile. Ensure your platform is set to at least iOS 13.0.

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '13.0'
    use_frameworks!
    
    target 'MyApp' do
      pod 'JKSwiftExtension'
    end
  5. Install SnapKit via CocoaPods

    master

    To integrate SnapKit using CocoaPods, ensure you have CocoaPods installed (gem install cocoapods). Add SnapKit to your Podfile. Note that CocoaPods 1.1.0+ is required for SnapKit 4.0.0+.

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '10.0'
    use_frameworks!
    
    target '<Your Target Name>' do
        pod 'SnapKit', '~> 5.7.0'
    end

    Then run:

    $ pod install
  6. Quick Start with SnapKit Auto Layout

    master

    SnapKit provides a DSL for Auto Layout. After importing SnapKit, use the .snp.makeConstraints method on a view to define its constraints relative to other views or its superview.

    import SnapKit
    
    class MyViewController: UIViewController {
    
        lazy var box = UIView()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.addSubview(box)
            box.backgroundColor = .green
            box.snp.makeConstraints { (make) -> Void in
               make.width.height.equalTo(50)
               make.center.equalTo(self.view)
            }
        }
    
    }
  7. Handle text input restrictions in UITextField and UITextView

    master

    To restrict user input (e.g., character limits, specific character types) in UITextField or UITextView, use the inputRestrictions method.

    Important for Paste Handling: If you need to process pasted content (e.g., to strip newlines or enforce limits on pasted text), you must subclass the standard components:

    • Use JKPastedTextField instead of UITextField.
    • Use JKPastedTextView instead of UITextView.

    After subclassing, set isRemovePasteboardNewlineCharacters to enable effective paste processing.

    // Example of subclassing for paste support
    class MyTextField: JKPastedTextField {
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.isRemovePasteboardNewlineCharacters = true
            self.inputRestrictions(...) // Apply restrictions
        }
    }
  8. Implement Dark Mode color adaptation with UIColor.darkModeColor

    master

    For non-layer colors, use UIColor.darkModeColor(lightColor:darkColor:). This method automatically handles the traitCollection to return the correct color based on the user's interface style on iOS 13+.

    // Define a color that adapts to light/dark mode
    extension UIColor {
        private(set) static var cB1: UIColor = UIColor.darkModeColor(lightColor: .green, darkColor: .blue)
    }
    
    // Usage
    self.view.backgroundColor = .cB1
  9. Use String length measurement with StringTypeLength

    master

    The String+Extension provides a typeLengh(_:) method that allows you to measure string length using different encoding strategies via the StringTypeLength enum.

    Available Length Types:

    • .count: Unicode character count.
    • .utf8: Length in UTF-8 bytes.
    • .utf16: Length corresponding to NSString.length.
    • .unicodeScalars: Count of Unicode scalars.
    enum StringTypeLength {
        case count
        case utf8
        case utf16
        case unicodeScalars
    }
    
    // Usage
    let length = myString.typeLengh(.utf8)