Latest 0.2.0
Homepage https://github.com/junmo-kim/JKAttributedString
License MIT
Platforms ios 8.0
Authors

JKAttributedString provides Swifty API for NSAttributedString.

Build Status

Sick of using NSAttributedString and NSMutableAttributedString on value typed Swift world?
This makes dealing with attributed string as easy as String.

Features

  • Value typed attributed string
  • Typed attributes
  • Easy concatination
  • Character based range (not UTF-16 based NSRange!)
  • Range converter between UTF-16 and character
  • Support custom attributes

How to Use

Basic Usage

var aString = JKAttributedString(string: "Whatever",
                                 attributes: [.color(.gray), .font(.systemFont(ofSize: 12))])
aString.adding(attributes: [.color(.red)])  // Red "Whatever"

let bString = "Want".attributed([.font(.systemFont(ofSize: 20))])  // Size 20
// bString.adding( ... )  <- COMPILE ERROR

let cString = aString + " You " + bString
// Whatever You { color: red, size: 12 }Want{ size: 20 }

let dString = "Whenever You " + bString  // Size 20

let testString = "Doing 테스트 ".attributed([.color(.gray)])
    + "🎯 です。¶".attributed([.color(.blue)])
testString.attributes(at: testString.index(testString.startIndex, offsetBy: 9)) // Gray
testString.attributes(at: testString.index(testString.startIndex, offsetBy: 10)) // Blue

Custom Attribute Type

enum SystemFontAttribute: JKAttributeType {
    case normal(CGFloat)
    case bold(CGFloat)

    var name: String {
        return NSFontAttributeName
    }

    var value: Any {
        switch self {
        case .normal(let size):
            return UIFont.systemFont(ofSize: size)
        case .bold(let size):
            return UIFont.boldSystemFont(ofSize: size)
        }
    }
}

let systemString = "System".attributed([SystemFontAttribute.bold(20)])

Latest podspec

{
    "name": "JKAttributedString",
    "version": "0.2.0",
    "summary": "Value typed Attributed String",
    "homepage": "https://github.com/junmo-kim/JKAttributedString",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Junmo KIM": "[email protected]"
    },
    "social_media_url": "https://www.linkedin.com/in/junmo-kim/",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/junmo-kim/JKAttributedString.git",
        "tag": "0.2.0"
    },
    "source_files": "Sources/*.swift",
    "pushed_with_swift_version": "3.2"
}

Pin It on Pinterest

Share This