Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

anni.saini

macrumors newbie
Original poster
Jul 15, 2009
18
0
I tried to create NSFont using fontDescriptorWithFontAttributes but NSFont return's null.

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

NSDictionary *fontTraits = [NSDictionary dictionaryWithObjectsAndKeys:400, NSFontWeightTrait, 1.0, NSFontSlantTrait, nil];

//Create a dictionary with font attributes
NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys:mad:"Arial", NSFontFaceAttribute, fontTraits, NSFontTraitsAttribute, nil];

NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];

//Create a NSFont
NSFont *nsFont = [NSFont fontWithDescriptor:fontDescriptor size:12];

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

Regards,
anni
 
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

First line of code: Since when are 400 and 1.0 objects?
 
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.

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.
 
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.

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.
 
NSNumber *weight = [NSNumber numberWithFloat:1.0];
NSNumber *slant = [NSNumber numberWithFloat:0.5];
NSDictionary *fontTraits = [NSDictionary dictionaryWithObjectsAndKeys:weight, NSFontWeightTrait, slant, NSFontSlantTrait, nil];

//Create a dictionary with font attributes
NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys:mad:"Arial", NSFontFaceAttribute, fontTraits, NSFontTraitsAttribute, nil];

NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];

//Create a NSFont
NSFont *nsFont = [NSFont fontWithDescriptor:fontDescriptor size:12];

I tried this too still the prob is same :(
 
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?
 
NSFontManager

hi,

I created like this but its crashing for some font... BAD EXCESS error
bool italic =false;
bool weight = false;
NSString *nsFaceName = [NSMutableString string];

NSFontManager *nsFontManager = [NSFontManager sharedFontManager];

NSFontTraitMask mask;

if (italic) {
mask = NSItalicFontMask;
}else {
mask = NSUnitalicFontMask;
}

int fontWeight;

if (weight) {
fontWeight = 5;
}else {
fontWeight = 9;
}
if([nsFontManager fontNamed:nsFaceName hasTraits:mask])
{
NSFont *nsFont = [nsFontManager fontWithFamily:nsFaceName traits:mask weight:fontWeight size:m_pointSize];
}
else
{
NSFont *nsFont = [NSFont fontWithName:nsFaceName size:m_pointSize];
}

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.
 
Could you please send me some code snippet for NSFontManager or example link...

I think that would be helpful.

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?
 
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?

PS. I'd be surprised if your code really produced a "BAD EXCESS" error. If it did, then maybe you should try some moderation.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.