TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Nov 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Ashik uddin Ahmad.
AKAttributeKit is a new fun way to create NSAttributedString. Same power of it without the hassles of genarating! Use HTML-ish tags instead of finding ranges for setting attributes.
// ---- Native way to NSAttributedString ----//
var mStr = NSMutableAttributedString(string: "Hello Attributed String!")
var range = (mStr.string as NSString).rangeOfString("Hello")
mStr.addAttributes([
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont(name: "Arial", size: 25)!
], range: range)
// ---- AKAttributeKit way to NSAttributedString ----//
mStr = "<fg #f00><font Arial|25>Hello</font></fg> Attributed string!".toAttributedString()
// or
mStr = AKAttributeKit.parseString("<fg #f00><font Arial|25>Hello</font></fg> Attributed string!")
Check the example project and/or playground to see and play with AKAttributeKit.
To run the example project, clone the repo, and run pod install
from the Example directory first.
There are some other libraries to create NSAttributedString form HTML. This is not like that. AKAttributeKit focuses on native APIs and gives a straight way to implement those. The basic difference between AKAttribute Tags and HTML Tags are:
| (Bar)
-separated sequencial values.<fg #f00>Red text <u>with</fg> underline</u>
is works just fine.Tag | Attribute | Example |
---|---|---|
a | NSLinkAttributeName | <a http://google.com>Google</a> |
base | NSBaselineOffsetAttributeName | square<base 15>2</base> |
bg | NSBackgroundColorAttributeName | <bg #00ff00>Green</bg> or <bg 255|255|0>Yellow</bg> |
ex | NSExpansionAttributeName | <ex 5>WIDE</ex> |
fg | NSForegroundColorAttributeName | <fg #ff0000>Red</fg> or <fg 0|0|255>Blue</fg> |
font | NSFontAttributeName | Different <font Arial|18>Font</font> |
i | NSObliquenessAttributeName | <i 0.5>Italic</i> or <i 0.8>oblique</i> |
k | NSKernAttributeName | <k 20>Huge Space</k> |
sc ,sw | NSStrokeColorAttributeName, NSStrokeWidthAttributeName | <sc #f00><sw 2>Storked Text</sw></sc> |
t ,tc | NSStrikethroughStyleAttributeName, NSStrikethroughColorAttributeName | <t 1>Wrong</t> or <t><tc #f00>Wrong</t></tc> |
u ,uc | NSUnderlineStyleAttributeName, NSUnderlineColorAttributeName | <u>Important</u> or <u 1><uc #f00>Important</u></uc> |
Note:
tc
oruc
are not necessary normally. But when used, it needt
oru
respectively in scope to be applied. Similarly,sc
andsw
need to be coupled to imply a visible attribute change.
Here is the short list of parameter types and acceptable formats to use:
Type | Acceptable Formats | Tags to apply |
---|---|---|
Link | Any valid URL format | a |
Int | Any integer value supported by respective attribute | t , u |
Float | Any float value | base , ex , i , k , sw |
Color | 1. Hex formats: rgb , rgba , rrggbb , rrggbbaa with or without 0x or # prefix 2. Integer sequence: r|g|b|a param sequence where all params in sequence are Int ranges from 0-255. 3. Insert UIColor: Directly insert UIColor into swift string like <tag \(myColor)> where myColor is any UIColor other than colorWithPatternImage . | bg , fg , sc , tc , uc |
Font | 1. Param sequence: fontName|fontSize param sequence where fontName is String and fontSize is Float 2. Insert UIFont: Directly insert UIFont into swift string like <font \(myFont.asAKAttribute())> | font |
AKAttributeKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "AKAttributeKit"
Below is some TODOs that I have plan to implement. New ideas and/or help on current tasks are most welcome :D
Ashik uddin Ahmad, [email protected]
AKAttributeKit is available under the MIT license. See the LICENSE file for more info.