Neo
Neo

Reputation: 541

how to get character size?

Any help would be great, thank you in advance. Right now I'm hard coding each character size

such as A = 8px and B = 7px etc

and storing in a hash table

My current page have a menu with each category taken from a database

so the menu can change when needed

for example i could have 10 category in the menu section such as reviews, contact us and products.

The problem is the length of the menu section is 1000px in width

it would display correctly in some browser such as IE 7-10 and in chrome it would exceeding the length over flowing to the sides.

Depending on the browser it would a just the size of the character.

so each category would fit in the menu. Is their a method or some way to get the size of the a word so it would display on the major browsers correctly?

Thank you sorry about the wording and grammar English is not my first language.

Upvotes: 2

Views: 470

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

To do this you'd have to declare the dictionary and store it in Application. I'm going to leave that detail out, but the declaration might be Dictionary<char, int>. Then you'd need a chars array to list what characters you want to measure. It might be defined like this char[] chars = new char[] { 'a', 'b', ... }.

Again, both of those will need to be stored in Application.

Then to build the dictionary you might do something like this (in the Global.asax file):

var b = new Bitmap(500, 200);
var g = Graphics.FromImage(b);

for (int i = 0; i < chars.Length; i++)
{
    var len = g.MeasureString(chars[i] as string, new Font("Arial", 12));
    dict.Add(chars[i], len);
}

Upvotes: 2

Related Questions