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

rethish

macrumors member
Original poster
Oct 16, 2008
41
0
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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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];
 

rethish

macrumors member
Original poster
Oct 16, 2008
41
0
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.