Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
I'm trying to write a general purpose routine that will set the font using a
variable instead of the literal font name.
string fontName = "Arial";
CGBitmapContext.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman);
CGBitmapContext.SelectFont("Arial", fontSize, CGTextEncoding.MacRoman);
But it seems not to work.
This works fine for me. Please attach a complete, self-contained, test case that shows the problem on this bug report.
public static float TextWidth(string text, string fontName, float fontSize)
int width = 200;
int height = 200;
float centerX = width / 2;
float centerY = height / 2;
float start, end, textWidth;
//Create the graphics context
CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, width, height, 8, 4 * width,
//Set the font
ctx.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman); //fontName = "Arial"
//Get the texts current position
start = ctx.TextPosition.X;
//Set the drawing mode to invisible
//Draw the text at the current position
//Get the end position
end = ctx.TextPosition.X;
//Subtract start from end to get the text's width
textWidth = end - start;
Not sure what you mean with your test case. Are you getting different 'textWidth' values ?
I copy/pasted your code twice, one version accept the 'fontName' from the method argument, while the other use a string constant: "Arial".
When I call both:
Console.WriteLine (TextWidth ("Hello World", "Arial", 14));
Console.WriteLine (TextWidthArial ("Hello World", "Arial", 14));
I get the same value in the "Application Output"
I got 0 returned from the routine. I'll try again with the latest release of MT.
Still getting 0 returned from routine... I'm running MT 4.0.6, if that makes a difference.
Please re-open with a self-contained test case if you're still able to reproduce this.