Reputation: 11652
I've been having a hard time trying to get the right answers for my problem. And have spent numerous days searching online and in documentation and have found nothing.
I have a Text File that contains a bunch of text. And on one of those lines inside the file will contain some Font info like this:
Tahoma,12.5,Regular
Note that the font info will not always have the same font name, size or style so I can't just set it manually.
When this file is opened into my app, it will parse the contents (which I have mostly covered), I just need some help converting the above font string into an actual font object and then assigning that font to a control, i.e. a label etc...
Can somebody please help me with this?
Upvotes: 1
Views: 9656
Reputation: 14579
You will want to use the Font class. Assuming you use String.Split() to parse the text into an array you will want to take each part of the array and use it to create a Font object like:
string s = "Tahoma,12.5,Regular";
string[] fi = s.Split(',');
Font font = new Font(fi[0], fi[1],fi[2]);
I don't have a C# compiler on this Mac so it may not be exactly correct.
Example constructor:
public Font(
string familyName,
float emSize,
FontStyle style
)
Here you need to specify the second argument as a float, so cast the string to a float with:
(float)fi[1]
Next you need to lookup a FontStyle based on what fi2 is:
if (fi[2] == "Regular") {
// set font style
}
Upvotes: 2