How to create NSFont with descriptor

Discussion in 'Mac Programming' started by anni.saini, Mar 25, 2010.

  1. anni.saini macrumors newbie

    Joined:
    Jul 15, 2009
    #1
    I tried to create NSFont using fontDescriptorWithFontAttributes but NSFont return's null.

    I want to set the font name, size, weight and slant.

    Can any body tell me what is wrong with this code, please suggest a right way of creating the NSFont...

    Regards,
    anni
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    First line of code: Since when are 400 and 1.0 objects?
     
  3. anni.saini thread starter macrumors newbie

    Joined:
    Jul 15, 2009
    #3
    By mistake I wrote 400, I tried with 0.5 too. Only for "Arial Unicode MS" working fine for rest of the font its returning null.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    400, 0.5 or any other number, whether integer or decimal is not a number. It is a primitive type. Given that you don't know the difference I suggest you go and read up on basic Object-Oriented programming.

    To fix your problem you need to convert your primitive numbers to objects. NSNumber would be a good place to start.
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    You are calling the method dictionaryWithObjectsAndKeys. And that method, as the name should tell you, wants _objects_ and _keys_. So again: Is 400 an object? No, it isn't. Is 0.5 an object? No, it isn't. What kind of object would you use to represent a number like 0.5? Hint: 400 and 0.5 are both numbers, that should help you finding a suitable kind of object.
     
  6. anni.saini thread starter macrumors newbie

    Joined:
    Jul 15, 2009
    #6
    I tried this too still the prob is same :(
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Is "Arial" a valid name. In particular the documentation says it must be "The fully specified family-face name of the font.". For example if you call availableFontFamilies on a valid NSFontManager is that exact string in the array returned?
     
  8. anni.saini thread starter macrumors newbie

    Joined:
    Jul 15, 2009
    #8
    NSFontManager

    hi,

    I created like this but its crashing for some font... BAD EXCESS error
    In this code I observe the nsFaceName some time doesn't show the font name and when condition is occurred, after this my application crashes.

    Could you please send me some code snippet for NSFontManager or example link...

    I think that would be helpful.
     
  9. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    I don't think it would be helpful at all, because you seem to have no idea what you are actually doing. Start with nsFaceName: Try to explain to us _exactly_ what you are trying to achieve with it. Then compare what you want to achieve with what your code is doing. When your code does something different than what you want to achieve, fix your code.

    Have you stepped through your code with the debugger? If not, why not? If yes, what did you see? Do you know how to use the debugger?

    BTW. When I say "try to explain exactly what you are trying to achieve", that is not so that we know what you are trying to achieve - the intention is that by trying to explain it you clarify your own thought process and hopefully figure out yourself what is wrong.

    BTW. You know that when you write

    Code:
    if (...)
    {
        NSFont* theFont  = ...;
    }
    
    the variable is not available outside the if-block?
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    PS. I'd be surprised if your code really produced a "BAD EXCESS" error. If it did, then maybe you should try some moderation.
     

Share This Page