I am trying to show the same RTFData in both a textArea and a graphics object but the result is a different height of text in each.
As an example, I have a textarea (ta) and canvas (cnv) side by side, in the ta.textChanged event:
rtfData = Me.RTFDataMBS
cnv.Invalidate
and in a cnv.paint event, the somewhat convoluted:
If rtfData = Nil Then Return
Dim NSA As NSAttributedStringMBS
nsa = NSAttributedStringMBS.attributedStringWithRTF( rtfdata )
Dim c As CGContextMBS = CGContextMBS.contextWithCGContext(g.Handle(g.HandleTypeCGContextRef) )
c.SaveGState
c.ScaleCTM(1, 1)
Dim nsg As nsGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContextHandle(c.handle)
nsg.drawInRect( NSA, New NSRectMBS( 0, 0, Me.width, Me.height) )
c.RestoreGState
Typing multiple lines of text into the textarea results in a different height of the text in the canvas.
Curiously though, using the same NSAttributed string method to get the height of the text, results in the correct height.
Dim nsG As New NSGraphicsMBS
Dim options As Integer = NSGraphicsMBS.NSStringDrawingUsesLineFragmentOrigin + NSGraphicsMBS.NSStringDrawingUsesFontLeading
Dim size As New NSSizeMBS
size.width = ta.width
Dim nsa As NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithRTF( RTFData )
If nsa <> Nil Then
Dim rect As NSRectMBS = nsG.boundingRectWithSize( nsa, size, options)
cnv.height = rect.Height
ta.height = rect.height
End If