All Projects → kruil → Prestyler

kruil / Prestyler

Licence: MIT license
Elegant text formatting tool in Swift 🔥

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Prestyler

Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (+716.67%)
Mutual labels:  string, text, nsattributedstring
Swiftrichstring
👩‍🎨 Elegant Attributed String composition in Swift sauce
Stars: ✭ 2,744 (+7522.22%)
Mutual labels:  text, nsattributedstring
Textwrap
An efficient and powerful Rust library for word wrapping text.
Stars: ✭ 164 (+355.56%)
Mutual labels:  text, formatting
React Element To Jsx String
Turn a ReactElement into the corresponding JSX string
Stars: ✭ 349 (+869.44%)
Mutual labels:  string, formatting
Csconsoleformat
.NET C# library for advanced formatting of console output [Apache]
Stars: ✭ 296 (+722.22%)
Mutual labels:  text, formatting
Decoro
Android library designed for automatic formatting of text input by custom rules
Stars: ✭ 325 (+802.78%)
Mutual labels:  text, formatting
kirai
String formatting library for Java, Android, Web and Unix Terminal
Stars: ✭ 69 (+91.67%)
Mutual labels:  string, formatting
html-comment-regex
Regular expression for matching HTML comments
Stars: ✭ 15 (-58.33%)
Mutual labels:  string, text
justified
Wrap, align and justify the words in a string.
Stars: ✭ 30 (-16.67%)
Mutual labels:  string, text
Typeset
Deal with AttributedString efficiently
Stars: ✭ 458 (+1172.22%)
Mutual labels:  string, nsattributedstring
humanize
A collection of utility functions, with built-in localization, for humanizing various types of data input
Stars: ✭ 73 (+102.78%)
Mutual labels:  text, formatting
dobbi
An open-source NLP library: fast text cleaning and preprocessing
Stars: ✭ 21 (-41.67%)
Mutual labels:  string, text
AutoFormatInputWatcher
This repository contains input watcher for auto formatting digits in edit text
Stars: ✭ 15 (-58.33%)
Mutual labels:  text, formatting
Styledtextkit
Declarative building and fast rendering attributed string library.
Stars: ✭ 1,171 (+3152.78%)
Mutual labels:  text, nsattributedstring
Colorful
Terminal string styling done right, in Python 🐍 🎉
Stars: ✭ 456 (+1166.67%)
Mutual labels:  string, style
react-native-styled-text
Styled Text for React Native
Stars: ✭ 57 (+58.33%)
Mutual labels:  text, style
textics
📉 JavaScript Text Statistics that counts lines, words, chars, and spaces.
Stars: ✭ 36 (+0%)
Mutual labels:  string, text
css-render
Generating CSS using JS with considerable flexibility and extensibility, at both server side and client side.
Stars: ✭ 137 (+280.56%)
Mutual labels:  style
prototyped.js
Some common Typescript prototypes
Stars: ✭ 22 (-38.89%)
Mutual labels:  string
svensktext
Svenska språkresurser: kvinno- och mansnamn, orter, län, kommuner, länder, nationaliteter, yrken, sentimentlexikon, moral, stoppord, myndigheter m.m.
Stars: ✭ 54 (+50%)
Mutual labels:  text

Prestyler: Swift text styler

CI Status Swift Version Documentation Carthage Compatible License Size Version Platform

Prestyler is a text styling library which is based on original NSAttributedString class. It simplifies and extends original workflow, giving you more clean and short syntax. You can find full documentation here. Please check it out and leave your feedback!


Prestyler allows you to replace this code

let baseString = "It is a pain to use attributed strings in ios."
let attributedString = NSMutableAttributedString(string: baseString, attributes: nil)
let painWord = (attributedString.string as NSString).range(of: "pain")
let attributes: [NSAttributedStringKey : Any] = [
    NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18),
    NSAttributedStringKey.underlineStyle : 2,
    NSAttributedStringKey.foregroundColor : UIColor.red]
attributedString.setAttributes(attributes, range: painWord)
label.attributedText = attributedString

to

Prestyler.defineRule("$", Prestyle.bold, Prestyle.underline, UIColor.red)
label.attributedText = "Prestyler does $everything$ for you.".prestyled()

Requirements and installation

  • iOS 9.0+
  • Xcode 10.0+
  • Swift 4.2+

CocoaPods is a dependency manager for Cocoa projects. To install Prestyler simply add the following line to your Podfile:

pod 'Prestyler'

Carthage is a decentralized dependency manager. To integrate Prestyler into your Xcode project specify it in Cartfile:

github "kruil/Prestyler" "1.1.0"

How to use

Prestyler parse your string, applies defined rules, and gives NSAttributedString as a result:

import Prestyler
...
label.attributedText = "Hello, i am <b>bold<b> text!".prestyled()

You also can style a text directly by listing its styles:

label.attributedText = "Prestyler".prestyledBy(styles: UIColor.green)

Or by defined rules:

Prestyler.defineRule("myRule", UIColor.green)
...
label.attributedText = "All this text is bold.".prestyledBy(rule: "myRule")

Predefined rules

Several rules already defined. It's a <b> for bold, <i> for italic, <strike> and <underline> .

label.attributedText = "And here is <i>italic<i> text!".prestyled()

Custom rules

You can easy define your own simple rules.

Prestyler.defineRule("$", UIColor.green)
label.attributedText = "It's a $green$ text.".prestyled()

or more complex with different styles combination:

Prestyler.defineRule("<BigYellowBold>", 48, UIColor.yellow, Prestyle.bold)
label.attributedText = "It's a <BigYellowBold>green<BigYellowBold> text.".prestyled()

When you define a rule first parameter is a pattern to search in String format, and then you put list of styles. To define a style you can use next classes:

* Prestyle.bold // .italic, .strike, .underline
* Precolor(.red) // Precolor("#af45392"), Precolor().random . Read more about colors below
* UIFont // UIFont.italicSystemFont(ofSize: 33)
* UIColor // UIColor.green
* String // "432" or "#432"or "#648362" treated as hex color
* Int // 18, treated as a font size

Prefilters

You can easy highlight numbers or other information usign Prefilters. It parses string and inserts provided tags which will be used later for styling. Now available two methods to highlight numbers and particular part of text. Both forms are used in this example:

// Filter what you need and style it
Prestyler.defineRule("*", UIColor.red)
let text = "Highlight text or numbers like 23 or 865 in your text using Prefilter. " +
           "It parses your text, embrace numbers 73 and 1234, and you are ready to go."
let prefilteredText = text.prefilter(type: .numbers, by: "*").prefilter(text: "text", by: "^")
label.attributedText = prefilteredText.prestyled()

Your image title

Working with colors 🎨

There are several ways you can manage colors in Prestyler. The easiest one is just to pass UIColor or hex string as a style:

Prestyler.defineRule("<colored>", UIColor.yellow)
Prestyler.defineRule("<colored>", "#b5e253")
Prestyler.defineRule("<colored>", "#ff0")
Prestyler.defineRule("<colored>", "ff2")

This colors would be applied to foreground text color. To set a background color you have to use Precolor class, which has several more interesting options.

Prestyler.defineRule("<colored>", Precolor(UIColor.yellow).forBackgound())
Prestyler.defineRule("<colored>", Precolor("#b5e253").forBackgound())
Prestyler.defineRule("<colored>", Precolor("#ff0").forBackgound())

Precolor has a random(_ percent: Int) method which allows you to get cool effects in seconds.

// Color is mixed for 50% with random color
Prestyler.defineRule("<randomRed>", Precolor(UIColor.red).random(50))
// 100% random
Prestyler.defineRule("<random>", Precolor().random())

Few things you need to know

  • Don't put a tag inside other tags. Results can be unexpectable (but safe). Instead create new Rule with desired style.
  • When you define already existing style the old one would replaced.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Prestyler: Swift text styler

Author

Ilia Krupko. I use Prestyler in my Monetal-ios project.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].