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.
- XCode 8.1
- Swift 3
- iOS 8.0+
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:
- AKAttribute tags are some custom defined tags based on NS**AttributeNames provided by native APIs. For convenience, related tags are named like in HTML, but they are not exact same thing.
- Properties for AKAttribute tags are provided as
| (Bar)
-separated sequencial values. - Unlike HTML, any tag started in other tag can be finished outside that tag. e.g.
<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
- For common tags with (almost) obvius choices make parameter optional.
- underline
- italic
- Strike through
- Support OSX
- Ability to escape a tag
- Unit testing
Ashik uddin Ahmad, ashikcu@gmail.com
AKAttributeKit is available under the MIT license. See the LICENSE file for more info.