Simple and lightweight Swift library that helps to define text style areas in given string with your own configuration, apply those styles and convert string into attributed string.
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'DownSwift'
end
Replace YOUR_TARGET_NAME
and then, in the Podfile directory, type:
$ pod install
Be careful with pod's version! Sometimes CocoaPods can miss the latest version within default installation so there are solutions:
pod update
in terminal within Podfile directory after pod's installation;OR
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
# Where '0.0.x' is a pod's version
pod 'DownSwift', '~> 0.0.x'
end
To add DownSwift to your project, select File → Swift Packages → Add Package Dependency
and enter the GitHub URL for DownSwift.
Check Adding Package Dependencies to Your App for detailed instructions.
First of all, import library and initialize main class:
import DownSwift
class YourBeautifulClass {
...
let downSwift = DownSwift()
...
}
Then create tags that will define text style areas in string:
// EXAMPLE
// For custom fonts I've used "Oswald-Regular" and "Oswald-Bold" fonts.
...
// `*` tag will define bold style area
downSwift.register(tag: "*", attributes: [
NSAttributedString.Key.font: customBoldFont,
NSAttributedString.Key.foregroundColor: UIColor.systemBlue
])
// `~` tag will define italic style area
downSwift.register(tag: "~", attributes: [
NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.systemRed
])
// `|` tag will define strikethrough style area
downSwift.register(tag: "|", attributes: [
NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.strikethroughColor: UIColor.gray
])
// To customize regular text style use `nil` in `tag` parameter!
downSwift.register(tag: nil, attributes: [NSAttributedString.Key.font: customRegularFont])
...
If you want to skip any characters in your string then use \\
.
After you created needed tags you can parse your string into attributed string with given style areas:
let string = "Hello and *welcome* to the ~example area!~ We are |sad| ~happy\\*~ that you are *here*!!!"
textLabel.attributedText = downSwift.parse(string)
Output:
DownSwift is available under the MIT license. Check LICENSE file for more info.
link |
Stars: 3 |
Last commit: 2 years ago |
Customisation logic has been changed so now it's easier for you to design your own style areas. Also library became almost weightless.
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics