Skip to content
forked from kruil/Prestyler

Elegant text formatting tool in Swift 🔥

License

Notifications You must be signed in to change notification settings

tonetime/Prestyler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prestyler: Swift text styler CI Status Version Swift Version License Platform

Prestyler is a text formatting library which is based on original NSAttributedString class. It simplifies and extends original workflow, giving you clean and short syntax.


Actually, 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 do $everything$ instead of you.".prestyled()

Requirements and installation

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

Prestyler is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Prestyler'

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

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 just create new Rule with desired style.
  • Adding same styles to rule like Prestyler.defineRule("$", UIColor.yellow, UIColor.green) has no effect. There will be applied just a last one.
  • 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

License

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

About

Elegant text formatting tool in Swift 🔥

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.3%
  • Ruby 4.7%