SemanticString

master

String abstraction for easy text localization and stylization
BlowMindStyle/SemanticString

Cocoapods platforms pod Swift Package Manager compatible

SemanticString

SemanticString is string abstraction that includes information about structural semantics for text. This information allows applying stylization to the text, forming a result of the NSAttributedString type.

Basic usage.

In the example we make "hello world!" text with highlighted word "world":

let text = SemanticString("Hello \(style: .bold, "world")!")
// or let text = SemanticString(xml: "Hello <bold>world</bold>!")

// object that provides text attributes
let provider = SemanticStringAttributesProvider(
   commonAttributes: [.font: UIFont.systemFont(ofSize: 14)],
   styleAttributes: [
       .bold: [.font: UIFont.boldSystemFont(ofSize: 14)]
   ]
)

let attributedText = text.getAttributedString(provider: provider)

Requirements

  • Swift 5.1

Installation

CocoaPods

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
   pod 'SemanticString'
end

Swift Package Manager

In XCode select File/Swift Packages/Add Package Dependency. Type 'SemanticString', select SemanticString project and click 'Next', 'Next'

Using with R.swift

If your app should support changing language at runtime, R.swift can be used to provide convenient access to string resources. SemanticString created from string resource not depends on the locale and can be used to update text on the screen. This lets you provide language-agnostic strings from services, which can be used to refresh UI without needing to recalculate data.

Code to add Rswift.StringResource conforming to SemanticString.StringResourceType:

import Rswift
import protocol SemanticString.StringResourceType

extension Rswift.StringResource: StringResourceType { }

Example of usage:

let string = SemanticString(resource: R.string.localizable.helloWorld)
print(string)
SemanticString.setCurrentLocale(Locale(identifier: "ru-RU"))
print(string)

Tip: Use typealias to shorten string resource paths.

typealias Strings = R.string.localizable

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Jun 28 2022 21:17:13 GMT-0500 (GMT-05:00)