Profile Picture

How to MeasureText for NCanvas?

Posted By Karl Hulme 9 Years Ago
Author
Message
Karl Hulme
Posted 9 Years Ago
View Quick Profile
Forum Guru

Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)

Group: Forum Members
Last Active: 9 Years Ago
Posts: 6, Visits: 16
I found NFont.MeasureText but this requires that the font object be attached/associated with a document.

I'm rendering text to the canvas in PrePaint using arg.PaintVisitor.PaintString and need a way to calculate the required height given a specific width.

Karl Hulme
This post has been flagged as an answer
Posted 9 Years Ago
View Quick Profile
Forum Guru

Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)Forum Guru (50 reputation)

Group: Forum Members
Last Active: 9 Years Ago
Posts: 6, Visits: 16
Oh dear! - There's an example of this in the Nevron Example app.

The code needs to look something like:...

            var txt = "Here is some text to measure";
            double targetWidth = 150;

            var settings = new NPaintTextRectSettings ();
            settings.SingleLine = false;
            settings.WrapMode = ENTextWrapMode.WordWrap;
            settings.HorzAlign = ENTextHorzAlign.Left;
            settings.VertAlign = ENTextVertAlign.Top;

            var resolution = canvas.Ownerdocument.GetEffectiveResolution ();
            var font = new NFont (NFontDescriptor.DefaultMonoFamilyName, 10);
            var textSize = font.MeasureString (txt.ToCharArray (), resolution, 150, false, ref settings);

            arg.PaintVisitor.SetFill (NColor.Black);
            arg.PaintVisitor.SetFont (font);
            arg.PaintVisitor.PaintString (
                new NRectangle(35, 35, textSize.Width, textSize.Height), 
                txt, ref settings);





Similar Topics


Reading This Topic