SwiftRichString

repository·master·Indexed 25 days ago

https://github.com/malcommac/swiftrichstring

A lightweight library for creating and manipulating attributed strings in Swift, supporting iOS, macOS, tvOS, and watchOS. It provides declarative styling via the Style class, XML/HTML tag-based rendering with StyleXML, and regular expression-based styling with StyleRegEx. The library includes features for global style registration via StyleManager, support for dynamic type, text transforms, and the ability to embed local or remote images in text.

Tokens
5.8K
Snippets
11
Records
21
Agent score
35%

What's inside SwiftRichString

  1. Install SwiftRichString

    master

    You can install SwiftRichString using CocoaPods, Swift Package Manager, or Carthage.

    ### CocoaPods
    ```ruby
    pod 'SwiftRichString'

    Swift Package Manager

    dependencies: [
        .package(url: "https://github.com/malcommac/SwiftRichString.git", from: "3.5.0")
    ]

    Carthage

    github "malcommac/SwiftRichString"
  2. Render XML/HTML tagged strings with `StyleXML`

    master

    SwiftRichString can parse and render XML-tagged strings into NSAttributedString instances. To do this:

    1. Define a base Style for the entire string.
    2. Define specific Style objects for each tag (e.g., <b>, <i>).
    3. Create a StyleXML instance containing the base style and a dictionary mapping tag names to their respective styles.
    4. Apply the StyleXML to the source string using .set(style:).
  3. Assign styles via Interface Builder

    master

    SwiftRichString extends UILabel, UITextView, and UITextField with three additional properties that can be used in Interface Builder:

    1. styleName: String: Set this in IB to use a globally registered style name. The text already set in the control will be rendered using this style.
    2. style: StyleProtocol: Allows you to set a specific style instance to render the control's text.
    3. styledText: String: Use this property instead of attributedText to set new text that is automatically rendered using the currently assigned style.

    Supported style types:

    • Style: Applies attributes to the entire text.
    • StyleXML: Applies a base attribute and applies specific attributes when XML/HTML tags are encountered.
    • StyleRegEx: Applies a base attribute and applies attributes only to matches of a specified pattern.
  4. Register and Use Styles Globally with `StyleManager`

    master
    To avoid duplication and enable app-wide theming, you can register Style or StyleXML instances globally using Styles.register(_:style:). Once registered, you can apply a style to a string using its unique string identifier.
  5. Use Local and Remote Images in Text

    master

    SwiftRichString supports attaching images to attributed strings via direct AttributedString initialization or through XML tags.

    Direct Initialization

    • Local: AttributedString(image:bounds:)
    • Remote: AttributedString(imageURL:)

    XML Tags

    • <img named="resourceName" rect="x,y,w,h"/> for local assets.
    • <img url="http://..."/> for remote URLs.

    Lazy Loading

    Provide a custom imageProvider closure to StyleXML to intercept image requests and return custom images manually.

  6. Concatenate Strings and Attributed Strings

    master
    SwiftRichString provides overloaded + operators to simplify composing text. You can concatenate String, AttributedString (typealias for NSMutableAttributedString), and Style objects. Additionally, you can use Swift 5.1's function builders via AttributedString.composing to build complex attributed strings declaratively.
  7. Manually apply styles to UI controls

    master

    You can manually apply styles to UILabel, UITextView, or UITextField in code using the following patterns:

    Using the .set() method on a String: Convert a standard string to an attributed string by applying a style.

    Using the .style and .styledText properties: Assign a style instance directly to the control and then update the text via styledText.

    // manually set an attributed string
    self.label?.attributedText = (self.label?.text ?? "").set(myStyle)
    
    // manually set the style via instance
    self.label?.style = myStyle
    self.label?.styledText = "Updated text"
  8. Update text in UI controls using styledText

    master

    When using the styleName property set in Interface Builder, you should update the content of the control using the styledText property to ensure the style is applied to the new content.

    // use `styleName` set value to update a text with the style
    self.label?.styledText = "Another text to render" // text is rendered using specified `styleName` value.
  9. Render complex strings with StyleXML

    master
    To render strings containing XML/HTML-like tags, use StyleXML. A StyleXML acts as a container that maps specific tag names to Style instances. You can also define a base style that applies to the entire string, providing a default ground for all text.
  10. Derivate a new `Style` using `byAdding()`

    master

    You can create a new style that inherits all properties from an existing style and then override or add specific attributes using the byAdding() method.

    let initialStyle = Style {
    	$0.font = SystemFonts.Helvetica_Light.font(size: 15)
    	$0.alignment = right
    }
    
    // subStyle inherits initialStyle but overrides alignment and adds a color
    let subStyle = initialStyle.byAdding {
    	$0.alignment = center
    	$0.color = UIColor.red
    }
  11. Create a basic Style

    master

    Use the Style class to encapsulate text attributes like font, color, and alignment. It uses a builder pattern where you provide a closure to configure properties. You can use UIColor, HEX strings, or SystemFonts for typography.

    let style = Style {
    	$0.font = SystemFonts.AmericanTypewriter.font(size: 25)
    	$0.color = "#0433FF" // HEX string supported
    	$0.underline = (.patternDot, UIColor.red)
    	$0.alignment = .center
    }
    let attributedText = "Hello World!".set(style: style)