Tribute
所属分类:collect
开发工具:Swift
文件大小:0KB
下载次数:0
上传日期:2022-02-18 15:22:59
上 传 者:
sh-1993
说明: 通过编程创建NSAttributedString不一定是一件痛苦的事情,
(Programmatic creation of NSAttributedString doesn t have to be a pain,)
文件列表:
.travis.yml (201, 2022-02-18)
Cartfile (0, 2022-02-18)
Cartfile.private (56, 2022-02-18)
Cartfile.resolved (61, 2022-02-18)
Carthage/ (0, 2022-02-18)
Carthage/Build/ (0, 2022-02-18)
Carthage/Build/Mac/ (0, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/ (0, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Headers/ (0, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Headers/DSL.h (5623, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Headers/NMBExceptionCapture.h (207, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Headers/Nimble-Swift.h (14320, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Headers/Nimble.h (189, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Info.plist (881, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Modules/ (0, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Modules/Nimble.swiftmodule/ (0, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Modules/Nimble.swiftmodule/x86_64.swiftdoc (35152, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Modules/Nimble.swiftmodule/x86_64.swiftmodule (113940, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Modules/module.modulemap (146, 2022-02-18)
Carthage/Build/Mac/Nimble.framework/Nimble (880084, 2022-02-18)
Carthage/Build/Mac/Quick.framework/ (0, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Headers (24, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Modules (24, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Quick (22, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Resources (26, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/ (0, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/ (0, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Headers/ (0, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Headers/QCKDSL.h (9058, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Headers/Quick-Swift.h (10494, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Headers/Quick.h (290, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Headers/QuickConfiguration.h (1101, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Headers/QuickSpec.h (1943, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Modules/ (0, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Modules/Quick.swiftmodule/ (0, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Modules/Quick.swiftmodule/x86_64.swiftdoc (34292, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Modules/Quick.swiftmodule/x86_64.swiftmodule (53624, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Modules/module.modulemap (142, 2022-02-18)
Carthage/Build/Mac/Quick.framework/Versions/A/Quick (352896, 2022-02-18)
... ...
# Tribute [![Build Status](https://travis-ci.org/zats/Tribute.svg?branch=master)](https://travis-ci.org/zats/Tribute)
```swift
let string = NSMutableAttributedString().add("Hello ") {
$0.font = .systemFontOfSize(20)
$0.color = .redColor()
$0.underline = .StyleSingle
}.add("world ") {
$0.stroke = .Filled(width: 2)
$0.strokeColor = .orangeColor()
}.add("of Swift "){
$0.font = .systemFontOfSize(12)
$0.underline = nil
$0.URL = NSURL(string: "http://swift.org")!
}.add(UIImage(named: "swift")!)
```
![](https://raw.githubusercontent.com/zats/Tribute/master/docs/attributed-string.png)
Not bad comparing to
```swift
let string2 = NSMutableAttributedString()
string2.appendAttributedString(NSAttributedString(string: "Hello ", attributes: [
NSFontAttributeName: UIFont.systemFontOfSize(20),
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
NSForegroundColorAttributeName: UIColor.redColor()
]))
string2.appendAttributedString(NSAttributedString(string: "world ", attributes: [
NSFontAttributeName: UIFont.systemFontOfSize(20),
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
NSForegroundColorAttributeName: UIColor.redColor(),
NSStrokeColorAttributeName: UIColor.orangeColor(),
NSStrokeWidthAttributeName: -2
]))
string2.appendAttributedString(NSAttributedString(string: "of Swift ", attributes: [
NSFontAttributeName: UIFont.systemFontOfSize(12),
NSForegroundColorAttributeName: UIColor.redColor(),
NSStrokeColorAttributeName: UIColor.orangeColor(),
NSStrokeWidthAttributeName: -2
]))
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "swift")
string2.appendAttributedString(NSAttributedString(attachment: attachment))
```
# Design Goals
1. Word processor logic: appending a string should inherit last attributes.
1. Allow for easy customization of common properties, including toggle bold or change the font size.
1. Flatten paragraph style and attributes, no more 5 lines of code if all you wanted is to change text alignment.
1. Replace weird attributes with more reasonable versions (for example [`Attribute.Stroke`](https://github.com/zats/Tribute/blob/master/Tribute/Tribute.swift#L24-L27) vs [NSStrokeWidthAttributeName](https://developer.apple.com/library/mac/qa/qa1531/_index.html)).
1. Minimal overhead: produce only required attributes.
1. Have an attributed string ready to use every time you leave the configuration block.
1. Replace string constants with strongly typed enums where possible.
# Notes
## Playground
Playgrounds do not always render `NSAttributesString` correctly (font variations and attachments are few of the problematic I noticed).
**Workaround**: Use a `UILabel` as a live view instead: `XCPlaygroundPage.currentPage.liveView = label`.
## Font variations
Not all fonts have both italic and bold variations
**Workaround**: Use `obliqueness` property as a poor man italic, and `expansion` as a bold respectively.
## Closure with one statement
When the configuration closure includes only one statement, compiler gets confused
**Workaround**: Specify closure type explicitly like so
```swift
string.add("text") { (inout a: Attributes) in
a.color = .redColor()
}
```
If you know a better way, please open a PR, I'd love to learn from it!
# Roadmap
- [ ] Objective-C compatibility
- [ ] Moar attributes (PRs are welcome)
*This is just a tribute*
近期下载者:
相关文件:
收藏者: