Bug 434 - Using variable instead of Font name literal does not work
Summary: Using variable instead of Font name literal does not work
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: monotouch@xamarin.com
Depends on:
Reported: 2011-08-25 13:58 UTC by ric3kg
Modified: 2013-06-02 13:01 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:

Description ric3kg 2011-08-25 13:58:44 UTC
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);
instead of:

   CGBitmapContext.SelectFont("Arial", fontSize, CGTextEncoding.MacRoman);
But it seems not to work.

MT 4.0.5
Comment 1 Sebastien Pouliot 2011-08-27 11:27:27 UTC
This works fine for me. Please attach a complete, self-contained, test case that shows the problem on this bug report.
Comment 2 ric3kg 2011-08-27 12:02:24 UTC
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,
					CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
			//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;
			return textWidth;
Comment 3 Sebastien Pouliot 2011-08-28 10:51:40 UTC
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"

Comment 4 ric3kg 2011-08-29 11:38:42 UTC
I got 0 returned from the routine. I'll try again with the latest release of MT.
Comment 5 ric3kg 2011-08-29 12:20:22 UTC
Still getting 0 returned from routine... I'm running MT 4.0.6, if that makes a difference.

Comment 6 Sebastien Pouliot 2013-06-02 13:01:06 UTC
Please re-open with a self-contained test case if you're still able to reproduce this.