All Projects → kazuhiro4949 → StringStylizer

kazuhiro4949 / StringStylizer

Licence: MIT license
Type strict builder class for NSAttributedString.

Programming Languages

swift
15916 projects
objective c
16641 projects - #2 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to StringStylizer

CuteAttribute
An elegant way to deal with attributed string in swift.
Stars: ✭ 65 (-13.33%)
Mutual labels:  nsattributedstring
Typeset
Deal with AttributedString efficiently
Stars: ✭ 458 (+510.67%)
Mutual labels:  nsattributedstring
Swiftyattributes
A Swifty API for attributed strings
Stars: ✭ 1,303 (+1637.33%)
Mutual labels:  nsattributedstring
Transformer
Easy Attributed String Creator
Stars: ✭ 278 (+270.67%)
Mutual labels:  nsattributedstring
Commonmarkattributedstring
Create NSAttributedStrings from Markdown Text
Stars: ✭ 382 (+409.33%)
Mutual labels:  nsattributedstring
Atributika
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
Stars: ✭ 845 (+1026.67%)
Mutual labels:  nsattributedstring
AttributedStringBuilder
Easily construct attributed strings in swift
Stars: ✭ 69 (-8%)
Mutual labels:  nsattributedstring
Swiftrichstring
👩‍🎨 Elegant Attributed String composition in Swift sauce
Stars: ✭ 2,744 (+3558.67%)
Mutual labels:  nsattributedstring
Nudein
An easy-to-use attributed text view for iOS Apps,use like masonry
Stars: ✭ 450 (+500%)
Mutual labels:  nsattributedstring
Styledtextkit
Declarative building and fast rendering attributed string library.
Stars: ✭ 1,171 (+1461.33%)
Mutual labels:  nsattributedstring
Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (+292%)
Mutual labels:  nsattributedstring
Nerdyui
An easy way to create and layout UI components for iOS.
Stars: ✭ 381 (+408%)
Mutual labels:  nsattributedstring
Gaudi
A swift theming framework for UIKit for easy adoption of multi theme and iOS dark mode
Stars: ✭ 36 (-52%)
Mutual labels:  nsattributedstring
Cupcake
An easy way to create and layout UI components for iOS (Swift version).
Stars: ✭ 273 (+264%)
Mutual labels:  nsattributedstring
Cjlabel
A drop-in replacement for UILabel that supports NSAttributedString, rich text, display any view, links, select copy and more
Stars: ✭ 140 (+86.67%)
Mutual labels:  nsattributedstring
Prestyler
Elegant text formatting tool in Swift 🔥
Stars: ✭ 36 (-52%)
Mutual labels:  nsattributedstring
Nsattributedstringbuilder
Composing NSAttributedString with SwiftUI-style syntax
Stars: ✭ 660 (+780%)
Mutual labels:  nsattributedstring
AttributedStringWrapper
a simple packaging for NSAttributedString to make the developers easy to use
Stars: ✭ 16 (-78.67%)
Mutual labels:  nsattributedstring
Bostring
Create NSAttributedString like a boss!
Stars: ✭ 193 (+157.33%)
Mutual labels:  nsattributedstring
Allkit
🛠 Async List Layout Kit
Stars: ✭ 40 (-46.67%)
Mutual labels:  nsattributedstring

StringStylizer

Carthage compatible Cocoapods compatible Swift 5.0

Type strict builder class for NSAttributedString.

What's this?

StringStylizer makes NSAttributedString more intuitive with Method chain and Operator. Building NSAttributedString is so difficult because it requires us to remember attribute names and types. If you do that with StringStylizer, There is no need to remember them 😃

NSAttributedString has the following format.

let attr: [NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.white,
    .font: UIFont(name: "HelveticaNeue", size: 14)
]
let str = NSAttributedString(string: "some text", attributes: attr)

StringStylizer enable developers to read and write their code in a linear manner. If you wanna convert String to NSAttributedString which has some colors, sizes and fonts, you can write that as follows.

let str = "some text".stylize().color(.white).size(14).font(.HelveticaNeue).attr

Feature

  • Type strict format
  • Assigning ranges and attributes in a linear manner
  • More readable than NSAttributedString

Requirements

  • iOS 8.0+
  • Swift 5.0~ or Swift 4.0~ or Swift 3.2~

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding StringStylizer as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/vanvo/StringStylizer.git", .upToNextMajor(from: "5.2.2"))
]

Carthage

  • Install Carthage from Homebrew
> ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
> brew update
> brew install carthage
  • Move your project dir and create Cartfile
> touch Cartfile
  • add the following line to Cartfile
github "kazuhiro4949/StringStylizer"
  • Create framework
> carthage update --platform iOS
  • In Xcode, move to "Genera > Build Phase > Linked Frameworks and Library"
  • Add the framework to your project
  • Add a new run script and put the following code
/usr/local/bin/carthage copy-frameworks
  • Click "+" at Input file and Add the framework path
$(SRCROOT)/Carthage/Build/iOS/StringStylizer.framework
  • Write Import statement on your source file
import StringStylizer

CocoaPods

  • Install CocoaPods
> gem install cocoapods
> pod setup
  • Create Podfile
> pod init
  • Edit Podfile
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'  # add
use_framework!  # add

target 'MyAppName' do
  pod 'StringStylizer' # add
end

target 'MyAppTests' do

end

target 'MyAppUITests'
  • Install
> pod install

open .xcworkspace

Example

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))

// build NSAttributedString.
let greeting = "Hi, ".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr

// build NSAttributedString with ranges.
let msg = "something happened ".stylize()
    .range(0..<9)        .color(0x009911).size(12).font(.HelveticaNeue)
    .range(10..<UInt.max).color(0xaa22cc).size(14).font(.HelveticaNeue_Bold).attr

// build NSAttributedString objects and join them.
let name = "to ".stylize().color(0x23abfc).size(12).font(.HelveticaNeue).attr +
    "you".stylize().color(0x123456).size(14).font(.HelveticaNeue_Italic).underline(.double).attr
    
// build NSAttributedString objects with strikethrough and kerning applied.
let response = "\nHow ".stylize().attr +  "boring".stylize().strikeThrough(.single).attr +
    " exciting!".stylize().kern(-2).attr

This sample generates a styled label.

Of course, you can wrap up the method chains.

extension StringStylizer {
  func strong() -> NSAttributedString {
    return self
            .color(0x123456)
            .size(14)
            .font(.HelveticaNeue_Italic)
            .underline(.double)
            .attr
  }
}

label.attributedText = "you".stylize().strong()

Usage

1. Convert String to StringStylizer object

let firstStep = "yay!".stylize() // => StringStylizer<Styling>
1a. Alternatively, use an optional String
let optionalString:String? = nil
let firstStep = optionalString.stylize() // => StringStylizer<Styling>

2. Call methods to select range. Then, StringStylizer change into "NarrowDown" state

let secondStep = "yay!".stylize().range(0..<UInt.max) // => StringStylizer<NarrowDown>

3. Call methods to set attributes. Then, StringStylizer change into "Styling" state

let thirdStep = "yay!".stylize().range(0..<UInt.max).size(14) // => StringStylizer<Styling>

4. Convert to NSAttributedString object.

let fourthStep = "yay!".stylize().range(0..<UInt.max).size(14).attr // => NSAttributedString

5. Join another NSAttributedString object.

let one     = "yay!".stylize().range(0..<UInt.max).size(14).attr
let another = " yay!".stylize().color(0xffffff).attr
let fifthStep = one + another // => NSAttributedString

That's it!

Architecture

StringStylizer is based on "Builder Pattern" (Effective Java version). In addition, it has states managed by "Phantom Type".

Because of them, we are able to

  • write our code in a linear manner
  • call proper methods depending on the situation.

License

Copyright (c) 2016 Kazuhiro Hayashi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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].