ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - rosberry/texstyle: Format iOS attributed strings easily
Format iOS attributed strings easily. Contribute to rosberry/texstyle development by creating an account on GitHub.
Visit Site

GitHub - rosberry/texstyle: Format iOS attributed strings easily

GitHub - rosberry/texstyle: Format iOS attributed strings easily

Texstyle allows you to format attributed strings easily.

Features

  • Applying attributes with strong typing and autocompletion
  • Cache for attributes
  • Substyles
  • Convenient extensions for String and UIButton

Requirements

  • iOS 9.0+ / tvOS 9.0+
  • Xcode 10.0+

Usage

Here is a basic example of Texstyle using:

let text = Text(value: "Hello, World 🌍", style: .title1)
text.add(.heading1, for: "World")
titleLabel.attributedText = text.attributed

The result:

Example1

Let's start with text styles. There is a TextStyle class for configuring different style parameters like font, color, kerning etc. We prefer to use TextStyle extension for app specific styles:

extension TextStyle {

    static let title1: TextStyle = {
        let style = TextStyle()
        style.font = .systemFont(ofSize: 32, weight: .regular)
        return style
    }()
        
    static let heading1: TextStyle = {
        let style = TextStyle()
        style.font = .systemFont(ofSize: 28, weight: .semibold)
        style.color = .purple
        return style
    }()
}

It allows you to reuse the styles across the app.

Next, create a Text instance. It contains string value and style:

let text = Text(value: "Hello, World 🌍", style: .title1)

Text also supports independent styles for each UIControl.State. For example, if you want to use a different style for button highlight state, you should initialize text like:

let text = Text(value: " Sign in with Apple", styles: [.normal: .heading1,
                                                         .highlighted: .heading2])

To create an attributed string, just use:

//By default for normal state
text.attributed

//For needed state
text.attributed(for: .highlighted)

All attributed strings are cached by default. It's useful in reusable elements like UICollectionViewCell or UITableViewCell.

There are custom operators for texts and strings:

let text1 = Text(value: "Ros", style: .title1)
let text2 = text1 + "ber"
let text3 = Text(value: "ry", style: .title1)
let text4 = text2 + text3
text4.value // "Rosberry"

There are convenience APIs for text creation and applying:

//String to text
"Settings".text(with: .heading1)

//UIButton
button.setText(text)

Check TexstyleExample project for more examples.

Example2

Cache

TextStyle and Text classes have internal cache to prevent extra calculation. TextStyle caches attributes and invalidates it on any attribute change. Text has cached attributed strings for states. For example, if you use texts in collection view cells and want to update some labels in it after reuse, texts return cached strings. Please note that texts won't invalidate cached strings if you update textstyle attributes.

Installation

Depo

Depo is a universal dependency manager that combines Carthage, SPM and CocoaPods and provides common user interface to all of them.

To install Texstyle via Carthage using Depo you need to add this to your Depofile:

carts:
  - kind: github
    identifier: rosberry/Texstyle
pods:
  - name: Texstyle

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamofire into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'Texstyle', '~> 0.0'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile:

github "Rosberry/Texstyle" ~> 5.0

Documentation

Read the docs. Generated with jazzy. Hosted by GitHub Pages.

About

This project is owned and maintained by Rosberry. We build mobile apps for users worldwide 🌏.

Check out our open source projects, read our blog or give us a high-five on 🐦 @rosberryapps.

License

Texstyle is available under the MIT license. See the LICENSE file for more info.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory