EmojiKit

repository·main·Indexed 41 days ago

https://github.com/danielsaidi/emojikit

A Swift library for Apple platforms providing emoji parsing, categorization, and versioning. It includes the Emoji model for metadata extraction, EmojiVersion for OS compatibility tracking, and SwiftUI components like EmojiGrid for displaying emojis. The library supports skin tone variants, localized names in English and Swedish, and conforms to Transferable for native drag-and-drop support.

Tokens
4.9K
Snippets
22
Records
32
Agent score
2%

What's inside EmojiKit

  1. Overview of EmojiKit features

    main

    EmojiKit is a Swift SDK designed for Apple platforms (iOS, macOS, tvOS, watchOS, and visionOS) using Swift and SwiftUI. It provides a comprehensive set of tools for handling emojis, including:

    • Emoji Models: Core models like Emoji, EmojiCategory, and EmojiVersion.
    • Unicode & Versions: Support for unicode and all available emoji versions.
    • Skin Tones: Support for skin tone information for compatible emojis.
    • Extensions: Native type extensions related to emojis.
    • Views: SwiftUI components specifically designed for emoji interaction, such as EmojiGrid and EmojiGridScrollView.
    • Localization: Built-in support for multiple languages.
  2. Configure namespaces for string keys

    main

    You can organize strings into a nested hierarchy by adding dots to a key. For example, a key named Experiments.DebugScreen.Title will be transformed into a nested accessor: .l10n.experiments.debugScreen.title.

    Namespaces help prevent key collisions when parsing multiple different string catalogs and reduce the risk of merge conflicts.

    // Example of generated hierarchy
    // Input key: Experiments.DebugScreen.Title
    // Output: .l10n.experiments.debugScreen.title
  3. How recent and frequent emoji categories are updated

    main

    The EmojiGrid automatically manages user history. When an emoji is picked via the grid, it calls Emoji.registerUserSelection() internally. This automatically updates the EmojiCategory.recent category.

    Note on Frequent Emojis: Currently, EmojiCategory.frequent is updated using the same logic as recent because EmojiKit does not yet implement a specific frequency calculation algorithm. You can replace this by implementing your own logic using the frequent category.

  4. How EmojiVersion works in EmojiKit

    main

    The EmojiVersion type is the core model used to manage emoji availability across different platforms and OS versions. It allows you to determine which emojis are safe to display based on the user's current runtime or a specific target OS.

    EmojiKit uses this versioning to ensure that emoji categories only return emojis that are actually available to the current runtime, preventing the display of 'tofu' (missing glyphs).

    // Get the latest version for the current runtime
    let version = EmojiVersion.current
    
    // Or target a specific version
    let v15 = EmojiVersion.v15
  5. Use EmojiGrid to list emojis and categories

    main

    The EmojiGrid component allows you to display a collection of Emoji or EmojiCategory values in either a horizontal or vertical grid.

    When using a vertical axis, the grid automatically inserts section titles before each category section if multiple categories are provided. To ensure the grid automatically scrolls to the current selection, wrap the EmojiGrid in an EmojiGridScrollView.

    // Example of wrapping in a scroll view for automatic scrolling
    EmojiGridScrollView {
        EmojiGrid(items: myEmojiItems)
    }
  6. Manage mutable emoji categories

    main

    Certain categories are mutable, allowing you to manage their contents. These include:

    • .favorites (via .favoriteEmojis)
    • .frequent (via .frequentEmojis)
    • .recent (via .recentEmojis)
    • .custom(id:name:emojis:iconName:) for user-defined categories.

    Use the following methods to manipulate these categories:

    • addEmoji(_:to:maxCount:): Add an emoji to a category.
    • removeEmoji(_:from:): Remove an emoji.
    • resetEmojis(for:): Clear a category.
    • setEmojis(_:for:maxCount:): Replace category contents.
    • getEmojisMaxCount(for:): Retrieve the current limit for a category.
    • setEmojisMaxCount(_:for:): Set a new limit for a category.

    Note: The EmojiGrid component automatically updates .frequent and .recent categories when a user selects emojis. Currently, .frequent behaves identically to .recent because a specific frequency algorithm is not yet implemented, but you can use the .frequent category to implement your own logic.

  7. Use the String Catalog Public Key Builder

    main

    The l10n-gen tool generates public key wrappers for internal, auto-generated keys in an Xcode string catalog. This allows you to access localized strings from other targets by using the .module bundle, ensuring proper localization regardless of where the string is called. It also supports nested string hierarchies via namespaces.

    # Run with help to see all usage examples
    swift run l10n-gen --help
    
    # Use the convenience script
    ./scripts/l10n-gen.script
  8. Scroll to a specific emoji

    main

    To programmatically scroll to a specific emoji:

    1. Using EmojiGridScrollView: Simply update the selection binding value provided to the grid. The scroll view will automatically adjust to the new selection.
    2. Using custom ScrollView: You must use the emoji.id(in:) property to find the correct ID. This is necessary because unique emoji IDs are adjusted based on their category when multiple categories are present. You can use ScrollViewProxy.scrollTo(_:) to perform the scroll.
  9. Access emojis by category using EmojiCategory

    main

    The EmojiCategory enum defines all available emoji categories. You can access the list of emojis within a specific category using the .emojis property. To get a list of all standard categories in their default sort order, use the .standard collection.

    EmojiCategory automatically filters emojis based on EmojiVersion, ensuring users only see emojis supported by their current device runtime.

    EmojiCategory.smileysAndPeople.emojis  // 😀😃😄...
    EmojiCategory.animalsAndNature.emojis  // 🐶🐱🐭...
    
    // Get all standard categories in order
    EmojiCategory.standard  // [.smileyAndPeople, .animalsAndNature, ...]