Skip to content

Commit 791ae49

Browse files
committed
Merge branch 'release/2.0.2'
2 parents a08773f + 8c02d45 commit 791ae49

File tree

19 files changed

+305
-96
lines changed

19 files changed

+305
-96
lines changed

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# macOS
12
.DS_Store
2-
/SwiftRichString/SwiftRichString.xcodeproj/xcuserdata/danielemargutti.xcuserdatad
3-
/SwiftRichString/SwiftRichString.xcodeproj/project.xcworkspace/xcuserdata/danielemargutti.xcuserdatad
4-
/SwiftRichString.xcworkspace/xcuserdata/danielemargutti.xcuserdatad
3+
4+
# Xcode
5+
xcuserdata
56
*.xcuserstate
7+
*.xctimeline

Configs/SwiftRichString.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0</string>
18+
<string>2.0.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

ExampleiOS/AppDelegate.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1818
// Override point for customization after application launch.
19-
20-
let bold = Style {
21-
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
22-
}
23-
let normal = Style {
24-
$0.font = SystemFonts.Helvetica_Light.font(size: 15)
25-
}
26-
let red = normal.byAdding {
27-
$0.color = UIColor.red
28-
$0.traitVariants = [TraitVariant.bold,TraitVariant.tightLineSpacing]
29-
}
30-
31-
Styles.register("MyStyle", style: StyleGroup(base: normal, ["bold" : bold, "red" : red]))
32-
3319
return true
3420
}
3521

ExampleiOS/ViewController.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class ViewController: UIViewController {
1414

1515
override func viewDidLoad() {
1616
super.viewDidLoad()
17-
18-
self.label?.styledText = "Hello <red>MERDA</red>"
1917
}
2018

2119
override func didReceiveMemoryWarning() {

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Other info:
133133

134134
### Versions
135135

136-
- **SwiftRichString 2.x branch (current)**. The latest version is [2.0.1](https://github.com/malcommac/SwiftRichString/releases/tag/2.0.1).
136+
- **SwiftRichString 2.x branch (current)**. The latest version is [2.0.2](https://github.com/malcommac/SwiftRichString/releases/tag/2.0.2).
137137
- **SwiftRichString 1.x branch (supported)**. Use [1.1.0 tag](https://github.com/malcommac/SwiftRichString/releases/tag/1.1.0). Its compatible with Swift 4.x.
138138
- **Swift 3.x (no longer mantained)**. Use [0.9.1 release](https://github.com/malcommac/SwiftRichString/releases/tag/0.9.10).
139139

@@ -487,6 +487,7 @@ The following properties are available:
487487
| font | `FontConvertible` | font used in text |
488488
| color | `ColorConvertible` | foreground color of the text |
489489
| backColor | `ColorConvertible` | background color of the text |
490+
| shadow | `NSShadow` | shadow effect of the text |
490491
| underline | `(NSUnderlineStyle?,ColorConvertible?)` | underline style and color (if color is nil foreground is used) |
491492
| strikethrough | `(NSUnderlineStyle?,ColorConvertible?)` | strikethrough style and color (if color is nil foreground is used) |
492493
| baselineOffset | `Float` | character’s offset from the baseline, in point |

Sources/SwiftRichString/Attributes/FontInfo.swift renamed to Sources/SwiftRichString/Attributes/FontData.swift

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ internal let WATCHOS_SYSTEMFONT_SIZE: CGFloat = 12.0
2121
/// User don't interact with this object directly but via `Style`'s properties.
2222
/// Using the `attributes` property this object return a valid instance of the attributes to describe
2323
/// required behaviour.
24-
internal struct FontInfo {
24+
public class FontData {
25+
26+
private static var DefaultFont = Font.systemFont(ofSize: 12.0)
2527

2628
/// Font object
27-
var font: FontConvertible { didSet { self.style?.invalidateCache() } }
29+
var font: FontConvertible? { didSet { self.style?.invalidateCache() } }
2830

2931
/// Size of the font
30-
var size: CGFloat { didSet { self.style?.invalidateCache() } }
32+
var size: CGFloat? { didSet { self.style?.invalidateCache() } }
3133

3234
#if os(OSX) || os(iOS) || os(tvOS)
3335

@@ -75,7 +77,9 @@ internal struct FontInfo {
7577

7678
/// Initialize a new `FontInfo` instance with system font with system font size.
7779
init() {
78-
#if os(tvOS)
80+
self.font = nil
81+
self.size = nil
82+
/*#if os(tvOS)
7983
self.font = Font.systemFont(ofSize: TVOS_SYSTEMFONT_SIZE)
8084
self.size = TVOS_SYSTEMFONT_SIZE
8185
#elseif os(watchOS)
@@ -84,18 +88,63 @@ internal struct FontInfo {
8488
#else
8589
self.font = Font.systemFont(ofSize: Font.systemFontSize)
8690
self.size = Font.systemFontSize
87-
#endif
91+
#endif*/
92+
}
93+
94+
/// Has font explicit value for font name or size
95+
var explicitFont: Bool {
96+
return (self.font != nil || self.size != nil)
8897
}
8998

9099
/// Return a font with all attributes set.
91100
///
92101
/// - Parameter size: ignored. It will be overriden by `fontSize` property.
93102
/// - Returns: instance of the font
94103
var attributes: [NSAttributedStringKey:Any] {
95-
var finalAttributes: [NSAttributedStringKey:Any] = [:]
104+
guard !self.explicitFont else {
105+
return [:]
106+
}
107+
return attributes(currentFont: self.font, size: self.size)
108+
}
109+
110+
/// Apply font attributes to the selected range.
111+
/// It's used to support ineriths from current font of an attributed string.
112+
/// Note: this method does nothing if a fixed font is set because the entire font attributes are replaced
113+
/// by default's `.attributes` of the Style.
114+
///
115+
/// - Parameters:
116+
/// - source: source of the attributed string.
117+
/// - range: range of application, `nil` means the entire string.
118+
internal func addAttributes(to source: AttributedString, range: NSRange?) {
119+
// This method does nothing if a fixed value for font attributes is set.
120+
// This becuause font attributes will be set along with the remaining attributes from `.attributes` dictionary.
121+
guard self.explicitFont else {
122+
return
123+
}
96124

125+
/// Enumerate fonts in string and attach the attributes
126+
let scanRange = (range ?? NSMakeRange(0, source.length))
127+
source.enumerateAttribute(.font, in: scanRange, options: []) { (fontValue, fontRange, shouldStop) in
128+
let currentFont = ((fontValue ?? FontData.DefaultFont) as? FontConvertible)
129+
let currentSize = (fontValue as? Font)?.pointSize
130+
let fontAttributes = self.attributes(currentFont: currentFont, size: currentSize)
131+
source.addAttributes(fontAttributes, range: fontRange)
132+
}
133+
}
134+
135+
/// Return the attributes by sending an already set font/size.
136+
/// If no fixed font/size is already set on self the current font/size is used instead, along with the additional font attributes.
137+
///
138+
/// - Parameters:
139+
/// - currentFont: current font.
140+
/// - currentSize: current font size.
141+
/// - Returns: attributes
142+
public func attributes(currentFont: FontConvertible?, size currentSize: CGFloat?) -> [NSAttributedStringKey:Any] {
143+
var finalAttributes: [NSAttributedStringKey:Any] = [:]
144+
97145
// generate an initial font from passed FontConvertible instance
98-
var finalFont = self.font.font(size: self.size)
146+
guard let size = (self.size ?? currentSize) else { return [:] }
147+
guard var finalFont = (self.font ?? currentFont)?.font(size: size) else { return [:] }
99148

100149
// compose the attributes
101150
#if os(iOS) || os(tvOS) || os(OSX)
@@ -113,7 +162,6 @@ internal struct FontInfo {
113162

114163
finalFont = finalFont.withAttributes(attributes)
115164

116-
117165
if let traitVariants = self.traitVariants { // manage emphasis
118166
let descriptor = finalFont.fontDescriptor
119167
let existingTraits = descriptor.symbolicTraits

Sources/SwiftRichString/Extensions/UIKit+Extras.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,14 @@ extension UITextView {
163163
}
164164

165165
#endif
166+
167+
//MARK: - compactMap for Swift 4.0 (not necessary > 4.0)
168+
169+
#if swift(>=4.1)
170+
#else
171+
extension Collection {
172+
func compactMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
173+
return try flatMap(transform)
174+
}
175+
}
176+
#endif

0 commit comments

Comments
 (0)