Reputation: 139
Cannot draw a String right justified in NSView ( MacOs ) Here is my code, which crashes with :
-[__SwiftValue lineBreakMode]: unrecognized selector sent to instance 0x600003408750
let attrs = [NSAttributedString.Key.foregroundColor:NSColor.white,
NSAttributedString.Key.paragraphStyle:NSTextAlignment.right
] as [NSAttributedString.Key : Any]
let absolutPoint = NSPoint(x: 0.0, y: 0.0)
var convertedPt = convertPoint(dataPoint:se,scaleX:scaleX,scaleY:scaleY)
convertedPt.x = absolutPoint.x
let scalaLabel = NSString(format:"%3.0f",se.y)
let place = NSMakeRect(convertedPt.x, convertedPt.y, 80.0, 12.0)
scalaLabel.draw(in:place ,withAttributes:attrs )
If I do not set paragraphStyle in attrs, text is drawn correctly. But not right-justified. Any idea what´s wrong with my code ?
Upvotes: 0
Views: 89
Reputation: 26096
NSAttributedString.Key.paragraphStyle:NSTextAlignment.right
That's not valid, that's why it's crashing.
The value should be a NSParagraphStyle
, not a NSTextAlignment
as stated in the documentation:
The value of this attribute is an
NSParagraphStyle
object. Use this attribute to apply multiple attributes to a range of text. If you do not specify this attribute, the string uses the default paragraph attributes, as returned by thedefault
method ofNSParagraphStyle
.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .right
let attrs = [NSAttributedString.Key.foregroundColor:NSColor.white,
NSAttributedString.Key.paragraphStyle: paragraphStyle
] as [NSAttributedString.Key : Any]
Side note, if you state the type before, it might be shorten:
let attrs: NSAttributedString.Key: Any] = [foregroundColor: NSColor.white,
paragraphStyle: paragraphStyle]
Upvotes: 5