What i'm trying to achieve is to draw some text on an image and achieve the same scaling(look) on images of varying sizes.
Example
Image 1
https://i.stack.imgur.com/SLZaV.png
Image 2
https://i.stack.imgur.com/TyUYJ.png
I'm setting a reference image when the NSTextView text is changed by the user,this image will serve as the reference.When the user selects a different image from a listbox,the reference values will be used for font size calculation.
override func controlTextDidChange(_ obj: Notification) {
if(obj.object is NSTextField)
{
let txtfield=obj.object as! NSTextField
if(txtfield.identifier?.rawValue=="txt_box")
{
setreferenceimage()
updatepreviewimage()
}
}
}
func setreferenceimage()
{
if(textscalemode == 2 && previewimage != nil)
{
referenceimagesize = previewimage.size
let fontAttributes = [NSAttributedStringKey.font: globalfont]
let fontsize = (text as NSString).size(withAttributes: fontAttributes)
referencetextsize=fontsize
referenceimagepath=selectedfilename
isreferenceset = true
panel_refimage.isHidden=false
}
}
Now when the user selects a different image i call the draw function which is supposed to scale the text.The font scaling is calculated like this
var yratio:CGFloat = max(referenceimagesize!.height, referencetextsize!.height) /
min(referenceimagesize!.height, referencetextsize!.height);
var newheight:CGFloat = 0;
newheight = image.size.height / yratio;
font = NSFont(name:font!.fontName, size: newheight)!
fontAttributes = [NSAttributedStringKey.font: font]
fontsize = (text as NSString).size(withAttributes: fontAttributes)
But this does not work .. see the samples below
The first image is the reference image .. as you can see the second image text does not have the adequate spacing required from the left
https://i.stack.imgur.com/v75nM.jpg