Skip to content

Commit b36a72b

Browse files
committed
#35 Fixes for font attributes
1 parent 770a54e commit b36a72b

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Sources/SwiftRichString/Attributes/FontData.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,19 @@ public class FontData {
9191
#endif*/
9292
}
9393

94+
/// Has font explicit value for font name or size
95+
var explicitFont: Bool {
96+
return (self.font != nil || self.size != nil)
97+
}
9498

9599
/// Return a font with all attributes set.
96100
///
97101
/// - Parameter size: ignored. It will be overriden by `fontSize` property.
98102
/// - Returns: instance of the font
99103
var attributes: [NSAttributedStringKey:Any] {
104+
guard !self.explicitFont else {
105+
return [:]
106+
}
100107
return attributes(currentFont: self.font, size: self.size)
101108
}
102109

@@ -111,8 +118,7 @@ public class FontData {
111118
internal func addAttributes(to source: AttributedString, range: NSRange?) {
112119
// This method does nothing if a fixed value for font attributes is set.
113120
// This becuause font attributes will be set along with the remaining attributes from `.attributes` dictionary.
114-
let fixedFont = (self.font != nil || self.size != nil)
115-
guard fixedFont else {
121+
guard self.explicitFont else {
116122
return
117123
}
118124

Sources/SwiftRichString/Style/Style.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public class Style: StyleProtocol {
128128
/// of the font point size. Specify 0 (the default) for no additional changes.
129129
/// Specify positive values to change the stroke width alone.
130130
/// Specify negative values to stroke and fill the text. For example, a typical value for
131-
/// outlined text would be 3.0.
131+
/// outlined text would be -3.0.
132132
public var stroke: (color: ColorConvertible?, width: Float?)? {
133133
set {
134134
self.set(attribute: newValue?.color?.color, forKey: .strokeColor)
@@ -602,7 +602,8 @@ public class Style: StyleProtocol {
602602
}
603603
// generate font from `fontInfo` attributes collection, then merge it with the inner attributes of the
604604
// string to generate a single attributes dictionary for `NSAttributedString`.
605-
self.cachedAttributes = self.innerAttributes.merging(self.fontData?.attributes ?? [:]) { (_, new) in return new }
605+
let fontAttributes = self.fontData?.attributes ?? [:]
606+
self.cachedAttributes = self.innerAttributes.merging(fontAttributes) { (_, new) in return new }
606607
return self.cachedAttributes!
607608
}
608609

Sources/SwiftRichString/Style/StyleProtocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public extension StyleProtocol {
5151
func set(to source: String, range: NSRange?) -> AttributedString {
5252
let attributedText = NSMutableAttributedString(string: source)
5353
self.fontData?.addAttributes(to: attributedText, range: nil)
54-
attributedText.setAttributes(self.attributes, range: (range ?? NSMakeRange(0, source.count)))
54+
attributedText.addAttributes(self.attributes, range: (range ?? NSMakeRange(0, source.count)))
5555
return attributedText
5656
}
5757

@@ -63,7 +63,7 @@ public extension StyleProtocol {
6363

6464
func set(to source: AttributedString, range: NSRange?) -> AttributedString {
6565
self.fontData?.addAttributes(to: source, range: range)
66-
source.setAttributes(self.attributes, range: (range ?? NSMakeRange(0, source.length)))
66+
source.addAttributes(self.attributes, range: (range ?? NSMakeRange(0, source.length)))
6767
return source
6868
}
6969

0 commit comments

Comments
 (0)