CascadeKit
CascadeKit is a library to apply styles to UIKit text controls based on the content language (to be precise, to the content script).
You can read more about this library on our Company Medium Blog.
Build Status
Branch | Status |
---|---|
Master |
Table of Contents
How to use CascadeKit
CascadeKit let you to easily apply UI customizations based on text content. Let's say you want to apply a blue color to Latin characters and a red color to Russian ones:
let mutAttrString = NSMutableAttributedString(string: text, attributes: originalAttributes)
let newAttr = mutAttrString.addAttributes(for: [.latin, .russian, .russianSupplementary], including: [.whiteSpace]) { (fallback) -> [Attribute] in
switch fallback.type {
case .latin:
return [
Attribute(key: .foregroundColor, value: UIColor.blue, range: fallback.range)]
case .russian, .russianSupplementary:
return [Attribute(key: .foregroundColor, value: red, range: fallback.range)]
default:
return []
}
}
textView.attributedText = newAttr
Easy, right?
We can enhance the example, applying more and more customisations to the Attributed String like
- Greek :
- foreground color = blue
- font = Helvetica Neue, 15
- Russian:
- background color = red
- foreground color = white
- font = Courier, 18
let mutAttrString = NSMutableAttributedString(string: text, attributes: originalAttributes)
let newAttr = mutAttrString.addAttributes(for: [.greek, .greekExtended, .russian, .russianSupplementary], including: [.whiteSpace]) { (fallback) -> [Attribute] in
switch fallback.type {
case .greek, .greekExtended:
return [
Attribute(key: .foregroundColor, value: UIColor.blue, range: fallback.range),
Attribute(key: .font, value: UIFont(name: "Helvetica Neue", size: 15), range: fallback.range)]
case .russian, .russianSupplementary:
return [
Attribute(key: .backgroundColor, value: UIColor.red, range: fallback.range),
Attribute(key: .foregroundColor, value: UIColor.white, range: fallback.range),
Attribute(key: .font, value: UIFont(name: "Courier", size: 18), range: fallback.range)]
default:
return []
}
}
textView.attributedText = newAttr
So the steps to setup your custom rules are:
- define the list of alphabets your UI needs to focus on, i.e.
let myAlphabets = [.greek, .greekExtended, .russian, .russianSupplementary]
- implement your custom rules into the
addAttributes
callback
Your beautifully crafted content is ready to be diplayed in each language of the world!
How to install CascadeKit
CocoaPods
Add CascadeKit to your Podfile
use_frameworks!
target 'MyTarget' do
pod 'CascadeKit', '~> 1.2.3'
end
$ pod install
Add CascadeKit source code to your project
Add CascadeKit folder to your project.
How to contribute
Contributions are greatly welcome
For any information or request feel free open an ISSUE.
License and Credits
License:
CascadeKit is available under the MIT license. See the LICENSE file for more info.