2

How can i specify font bold/italic etc. properties in monotouch?

Actually possible in native library http://www.freetimestudios.com/2010/09/20/ipad-and-ios-4-custom-font-loading/

NSDictionary *fontAttributes =
  [NSDictionary dictionaryWithObjectsAndKeys:
    @"Courier", (NSString *)kCTFontFamilyNameAttribute,
    @"Bold", (NSString *)kCTFontStyleNameAttribute,
    [NSNumber numberWithFloat:16.f], (NSString *)kCTFontSizeAttribute,
    nil];
CTFontDescriptorRef descriptor =
  CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);
CFRelease(descriptor);
Charles
  • 50,943
  • 13
  • 104
  • 142
Mesut A.
  • 1,618
  • 1
  • 10
  • 11
  • Does this other question (http://stackoverflow.com/questions/4913363/setting-a-custom-font-for-monotouch-dialog-elements) help at all? – summea Jan 25 '12 at 18:26
  • actually not helped for me. i need a bold text for custom fonts – Mesut A. Jan 25 '12 at 19:26
  • Have you read through the "Registering Custom Fonts With iOS" section on that link you listed earlier and verified that everything is correctly set with `UIAppFonts` in the `Info.plist`? – summea Jan 25 '12 at 19:41

1 Answers1

2

The MonoTouch / C# code to match your code snippet would look like this:

CTFontDescriptorAttributes fda = new CTFontDescriptorAttributes () {
    FamilyName = "Courier",
    StyleName = "Bold",
    Size = 16.0f
};
CTFontDescriptor fd = new CTFontDescriptor (fda);
CTFont font = new CTFont (fd, 0);
summea
  • 7,390
  • 4
  • 32
  • 48
poupou
  • 43,413
  • 6
  • 77
  • 174
  • thanks a lot but a new problem here. how can i use with uilabel/uibutton etc.. shortly how can i convert ctfont to uifont – Mesut A. Jan 26 '12 at 08:41
  • see http://stackoverflow.com/questions/6714858/iphone-convert-ctfont-to-uifont but note that not every attributes will be converted. However that will cover **Bold**. In C# this should look like `UIFont f = UIFont.FromName (font.PostScriptName, 16);` – poupou Jan 26 '12 at 12:57