@@ -93,16 +93,32 @@ public extension NSImage {
9393 return finalResult
9494 }
9595
96- func image( withText text: String , attributes: [ NSAttributedString . Key : Any ] , horizontalTitlePosition: CGFloat , verticalTitlePosition: CGFloat ) -> NSImage {
96+ func image(
97+ withText text: String ,
98+ alignmentMode: NSTextAlignment ,
99+ attributes: [ NSAttributedString . Key : Any ] ,
100+ horizontalTitlePosition: CGFloat ,
101+ verticalTitlePosition: CGFloat ) -> NSImage {
102+
97103 let image = self
98104 let text = text as NSString
99- // let options: NSString.DrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
105+ let options : NSString . DrawingOptions = [ . usesLineFragmentOrigin, . usesFontLeading]
100106
101- // let textSize = text.boundingRect(with: image.size, options: options, attributes: attributes).size
107+ let textSize = text. boundingRect ( with: image. size, options: options, attributes: attributes) . size
108+ var offsetX : CGFloat
109+
110+ switch alignmentMode {
111+ case . left:
112+ offsetX = 0
113+ case . center:
114+ offsetX = - textSize. width/ 2
115+ case . right:
116+ offsetX = - textSize. width
117+ default :
118+ offsetX = 0
119+ }
102120
103- // let x = (image.size.width - textSize.width) / 2
104- // let y = (image.size.height - textSize.height) / 2
105- let point = NSMakePoint ( image. size. width * horizontalTitlePosition, image. size. height * verticalTitlePosition)
121+ let point = NSMakePoint ( image. size. width * horizontalTitlePosition + offsetX, image. size. height * verticalTitlePosition - textSize. height/ 2 )
106122
107123 image. lockFocus ( )
108124 text. draw ( at: point, withAttributes: attributes)
@@ -144,6 +160,7 @@ public extension NSImage {
144160 // Solid color text
145161 let fillText = image (
146162 withText: text,
163+ alignmentMode: alignmentMode,
147164 attributes: [
148165 . foregroundColor: fill,
149166 . font: titleFont,
@@ -156,6 +173,7 @@ public extension NSImage {
156173 // Add strokes
157174 return fillText. image (
158175 withText: text,
176+ alignmentMode: alignmentMode,
159177 attributes: [
160178 . foregroundColor: NSColor . clear,
161179 . strokeColor: stroke,
0 commit comments