Drawn string changes in color but not in size

Discussion in 'Mac Programming' started by Hans Kamp, Apr 4, 2013.

  1. macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #1
    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?
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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...gs.html#//apple_ref/doc/uid/20000714-BBCCGGCC

    Google search terms: NSAttributedString class reference
     
  3. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #3
    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. :)
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    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.
     

Share This Page