diff --git a/Source/model/scene/Text.swift b/Source/model/scene/Text.swift index 31188329..f19314e2 100644 --- a/Source/model/scene/Text.swift +++ b/Source/model/scene/Text.swift @@ -67,7 +67,7 @@ open class Text: Node { let font: MFont if let f = self.font { - if let customFont = RenderUtils.loadFont(name: f.name, size: f.size) { + if let customFont = RenderUtils.loadFont(name: f.name, size: f.size, weight: f.weight) { font = customFont } else { font = MFont.systemFont(ofSize: CGFloat(f.size), weight: getWeight(f.weight)) diff --git a/Source/platform/iOS/Common_iOS.swift b/Source/platform/iOS/Common_iOS.swift index 4f418535..644efd59 100644 --- a/Source/platform/iOS/Common_iOS.swift +++ b/Source/platform/iOS/Common_iOS.swift @@ -13,6 +13,7 @@ import UIKit public typealias MRectCorner = UIRectCorner public typealias MFont = UIFont +public typealias MFontDescriptor = UIFontDescriptor public typealias MColor = UIColor public typealias MEvent = UIEvent public typealias MTouch = UITouch diff --git a/Source/platform/macOS/Common_macOS.swift b/Source/platform/macOS/Common_macOS.swift index 638e38a6..8de9818b 100644 --- a/Source/platform/macOS/Common_macOS.swift +++ b/Source/platform/macOS/Common_macOS.swift @@ -13,6 +13,7 @@ import Cocoa import Quartz public typealias MFont = NSFont +public typealias MFontDescriptor = NSFontDescriptor public typealias MColor = NSColor public typealias MEvent = NSEvent public typealias MTouch = NSTouch diff --git a/Source/render/RenderUtils.swift b/Source/render/RenderUtils.swift index 4f3462e8..e6513c51 100644 --- a/Source/render/RenderUtils.swift +++ b/Source/render/RenderUtils.swift @@ -92,26 +92,39 @@ class RenderUtils { static let availableFonts = MFont.mFamilyNames.map{ $0.lowercased() } - class func loadFont(name: String, size: Int) -> MFont? { - + class func loadFont(name: String, size: Int, weight: String?) -> MFont? { + + var fontName = "" let fontPriorities = name.split(separator: ",").map{ String($0).trimmingCharacters(in: CharacterSet(charactersIn: " '")).lowercased() } for font in fontPriorities { if availableFonts.contains(font) { - return MFont(name: font, size: CGFloat(size)) + fontName = font } if font == "serif" { - return MFont(name: "Georgia", size: CGFloat(size)) + fontName = "Georgia" } if font == "sans-serif" { - return MFont(name: "Arial", size: CGFloat(size)) + fontName = "Arial" } if font == "monospace" { - return MFont(name: "Courier", size: CGFloat(size)) + fontName = "Courier" } } - - return .none + if fontName.isEmpty { + return .none + } + + var fontDesc = MFontDescriptor(name: fontName, size: CGFloat(size)) + if weight == "bold" || weight == "bolder" { + #if os(iOS) + fontDesc = fontDesc.withSymbolicTraits(.traitBold)! + #elseif os(OSX) + fontDesc = fontDesc.withSymbolicTraits(.bold) + #endif + + } + return MFont(descriptor: fontDesc, size: CGFloat(size)) } class func applyOpacity(_ color: Color, opacity: Double) -> Color { diff --git a/Source/render/TextRenderer.swift b/Source/render/TextRenderer.swift index 45760dd5..85a6b988 100644 --- a/Source/render/TextRenderer.swift +++ b/Source/render/TextRenderer.swift @@ -76,14 +76,11 @@ class TextRenderer: NodeRenderer { // However it is needed for the Swift Package Manager to work accordingly. return MFont() } - guard let text = text else { - return MFont.systemFont(ofSize: 18.0) - } - guard let textFont = text.font else { + guard let text = text, let textFont = text.font else { return MFont.systemFont(ofSize: MFont.mSystemFontSize) } - if let customFont = RenderUtils.loadFont(name: textFont.name, size: textFont.size) { + if let customFont = RenderUtils.loadFont(name: textFont.name, size: textFont.size, weight: textFont.weight) { return customFont } else { if let weight = getWeight(textFont.weight) {