Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 4, 2013, 03:43 PM   #1
Hans Kamp
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Drawn string changes in color but not in size

Code:
    NSString *sNumber = [[NSString alloc] initWithFormat:@"%d", num]; // convert number into string
    
    NSMutableDictionary *attrib = [[NSMutableDictionary alloc] init]; // create a dictionary
    [attrib setObject:[NSColor colorWithDeviceRed:0 green:0.5 blue:0 alpha:1] forKey:NSForegroundColorAttributeName]; // change the color to dark green (works)
    [attrib setObject:@40 forKey:NSFontSizeAttribute]; // tries to change the size of the font to 40 (doesn't work)
        
    NSMutableAttributedString *masNumber = [[NSMutableAttributedString alloc] initWithString:sNumber attributes:attrib]; // create an attributed string with given string and attributes.
    
    NSSize omvang = [masNumber size]; // calculate the size
    NSLog(@"getal: %d, breedte: %f, hoogte: %f", num, omvang.width, omvang.height); // log the width, the height and the value (converted into string
    NSPoint startTekstPunt = NSMakePoint(tekstPunt.x - omvang.width / 2, tekstPunt.y - omvang.height / 2); // calculate were to draw
    [masNumber drawAtPoint:startTekstPunt]; // draw the string
The color of the string is dark green (no red and blue intensity but 50 % green intensity) but the size is ignored.

What did I do wrong?
Hans Kamp is offline   0 Reply With Quote
Old Apr 4, 2013, 09:29 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
The fundamental question you should ask yourself (then proceed to answer) is this:
Is NSFontSizeAttribute an attribute supported by NSAttributedString?


Some other questions you may wish to consider:

Are you following a tutorial or example? If so, what is the URL? Post it, so we know what you're following.

What reference docs have you read regarding NSAttributedString and its default supported attributes? If it's an online reference, what's the URL? If it's a book, what's the title, author, edition number, and page number? If it's Xcode's builtin docs, exactly which reference is it?

If you aren't doing any of those, and you haven't consulted a reference, what makes you think the code would work? I.e. explain your rationale.

Also see this:
https://developer.apple.com/library/...00714-BBCCGGCC

Google search terms: NSAttributedString class reference
chown33 is offline   0 Reply With Quote
Old Apr 5, 2013, 02:32 AM   #3
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Quote:
Originally Posted by chown33 View Post
The fundamental question you should ask yourself (then proceed to answer) is this:
Is NSFontSizeAttribute an attribute supported by NSAttributedString?
[...]
If you aren't doing any of those, and you haven't consulted a reference, what makes you think the code would work? I.e. explain your rationale.
I assumed the answer to be yes, because you can specify the color, the size and the font of the text you want to draw. Wrong assumption, obviously.

This may well be the solution to the problem. Font and size seem to be combined into one object, unlike other text properties.

Thanks for giving me a hint.
Hans Kamp is offline   0 Reply With Quote
Old Apr 5, 2013, 11:57 AM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
It wasn't just a hint, it was a process. To summarize the process:

1. Find a tutorial.
2. If you can't find a tutorial, find a working sample or example.
3. If you can't find a working sample, find the reference docs.
4. When asking a question, tell everyone how you got there (i.e. give the URL of the tutorial, sample, and/or reference doc).

An important part of learning how to program is learning how to learn new things by discovery. That means having a repeatable process for finding tutorials, examples, and reference docs, and being able to confirm or refute your assumptions by referring to those docs.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
What size and color iPhone 6 are you getting? Appl3FTW iPhone 237 Sep 13, 2014 11:43 AM
iPhone 5 - Size and Color stockcerts iPhone 0 Sep 13, 2012 01:27 PM

Forum Jump

All times are GMT -5. The time now is 08:53 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC