setting font size

Discussion in 'Mac Programming' started by rethish, Apr 30, 2009.

  1. rethish macrumors member

    Joined:
    Oct 16, 2008
    #1
    Hi,

    I am developing a text editor in which I use to set the font name and font size by selecting it from popup buttons.

    Fonts obtained using the code:
    NSArray *path=[[NSFontManager alloc] availableFonts];
    [fontList addItemsWithTitles:path];

    I use to set font using :
    NSFont *font = [NSFont fontWithName:[fontList titleOfSelectedItem] size:[[fontSize titleOfSelectedItem] doubleValue]];

    NSRange selectedRange= [textView rangeForUserCharacterAttributeChange];
    [textView setFont:font range:selectedRange ];

    This works properly if the selected text is composed of single font.
    But if i select the text which is composed of multiple fonts, then font of entire text is set to the font, that is last selected from the popupbutton.


    How can I resize the font size without affecting the fonts in it?

    Thank you in advance

    Rethish
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I'm guessing you'd have to loop through the NSAttributedString and get the font for each character. Then you could use -[NSFontManager convertFont:toSize:] to convert the font to a different size, and then reapply that font to the character.

    Edit: something like this :)
    Code:
    @interface NSMutableAttributedString (Additions)
    - (void)resizeTo:(CGFloat)size;
    @end
    @implementation NSMutableAttributedString (Additions)
    - (void)resizeTo:(CGFloat)size
    {
        NSFontManager *fm = [NSFontManager sharedFontManager];
        for (NSUInteger i=0; i<[self length]; i++)
        {
            NSDictionary *attrs = [self attributesAtIndex:i effectiveRange:NULL];
            NSFont *font = [fm convertFont:[attrs objectForKey:NSFontAttributeName] toSize:size];
            [self addAttribute:NSFontAttributeName value:font range:NSMakeRange(i, 1)];
        }
    }
    @end
    Example usage:
    Code:
    [[textView textStorage] resizeTo:18.0];
     
  3. rethish thread starter macrumors member

    Joined:
    Oct 16, 2008
    #3
    hi

    Thank you for your valuable advice.

    when i use this, entire string size is changed. I need to change the size of selected string.

    thank you.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Yes that was just an example. You need to modify it to work with the selection. It probably won't be a direct drop-in replacement though. Look at the docs - there is an entire section for "Managing the Selection".
     
  5. rethish thread starter macrumors member

    Joined:
    Oct 16, 2008
    #5
    thank you for your kind support in this topic.
     

Share This Page