Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed underlining when no explicit foreground color is set #37

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions DocX/AttributeElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ extension Dictionary where Key == NSAttributedString.Key{
attributesElement.addChildren(self.outlineProperties(strokeWidth: strokeWidth, font:font))
}

if let style=self[.underlineStyle] as? Int, let color=self[.foregroundColor] as? NSColor{
if let style=self[.underlineStyle] as? Int {
// If the `underlineColor` attribute is present, use that as the color
let underlineColor = self[.underlineColor] as? NSColor

let underline=NSUnderlineStyle(rawValue: style)
attributesElement.addChild(underline.underlineElement(for: color))
attributesElement.addChild(underline.underlineElement(for: underlineColor))
}

if let backgroundColor=self[.backgroundColor] as? NSColor{
Expand Down
14 changes: 11 additions & 3 deletions DocX/NSUnderlineStyle+Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@ extension NSUnderlineStyle{
return val
}

func underlineElement(for color:NSColor)->AEXMLElement{
let colorString=color.hexColorString
return AEXMLElement(name: "w:u", value: nil, attributes: ["w:color":colorString, "w:val":self.elementValue])
func underlineElement(for color:NSColor?)->AEXMLElement{
// Always add the underline value, which determines the underline style
var attributes = ["w:val": self.elementValue]

// If color is specified, include that too
if let color = color {
let colorString = color.hexColorString
attributes["w:color"] = colorString
}

return AEXMLElement(name: "w:u", value: nil, attributes: attributes)
}

var strikeThroughElement:AEXMLElement{
Expand Down