Git Product home page Git Product logo

texstyle's Introduction

Stylin

Swift Version Carthage Compatible codecov

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

Installation

Carthage

Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add the framework to your project.

github "rosberry/Texstyle"

Manually

Drag Sources folder from last release into your project.

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.

texstyle's People

Contributors

artemnovichkov avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.