[futurebasic] Re: [FB] subscript, superscript

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : February 2017 : Group Archive : Group : All Groups

From: Bernie <bernie.fblist@...>
Date: Fri, 24 Feb 2017 08:02:44 +0000
Steve wrote:

> /*
>    Demonstrates superscripts and subscripts
>    using NSMutableAttributedString
>    S.Van Voorst   {20170223}
> */

Thanks Steve.

I don’t know what the recommended font size ratio is for super/subscript, but 0.65 looks nice.

/*
   Demonstrates superscripts and subscripts
   using NSMutableAttributedString
   S.Van Voorst   {20170223}
*/
BeginCDeclaration
@interface MainWindow : NSObject
@end
EndC

BeginCFunction
@implementation MainWindow

- (void)attributedString:(NSMutableAttributedString *)string setSuperscriptInRange:(NSRange)range {
     NSFont *font = [string attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];
     NSFont *supFont = [NSFont fontWithName:font.fontName size:font.pointSize * 0.65];
     NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:supFont,NSFontAttributeName,[NSNumber numberWithInt:1],NSSuperscriptAttributeName,nil];
     [string addAttributes:attr range:range];
}

- (void)attributedString:(NSMutableAttributedString *)string setSubscriptInRange:(NSRange)range {
     NSFont *font = [string attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];
     NSFont *supFont = [NSFont fontWithName:font.fontName size:font.pointSize * 0.65];
     NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:supFont,NSFontAttributeName,[NSNumber numberWithInt:-1],NSSuperscriptAttributeName,nil];
     [string addAttributes:attr range:range];
}

- (void) buildWnd
{
// **** Menubar and Menu **** //
 NSMenu *menubar = [[NSMenu new] autorelease];
 NSMenuItem *appMenuItem = [[NSMenuItem new] autorelease];
 [menubar addItem:appMenuItem];
 [NSApp setMainMenu:menubar];
 NSMenu *appMenu = [[NSMenu new] autorelease];
 NSMenuItem *quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
        action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
 [appMenu addItem:quitMenuItem];
 [appMenuItem setSubmenu:appMenu];

// **** Window **** //
 #define _wndW  500
 #define _wndH  250
 NSWindow *window = [ [ NSWindow alloc ] 
                       initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
                       styleMask: NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMask
                       backing:NSBackingStoreBuffered
                       defer:YES];
 [window center];
 [window setTitle: @"Super/Subscripts" ];
 [window makeKeyAndOrderFront:nil]; 

NSTextView *txtView = [[NSTextView alloc] initWithFrame:NSMakeRect( 10, 60, _wndW-20, _wndH - 70 )];
[[window contentView]  addSubview: txtView];
[txtView release];

// ****** NSMutableAttributed String ******* //
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
[attr setObject:[NSFont fontWithName:@"Lucida Grande" size:24] forKey:NSFontAttributeName];
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithString: @"e = mc2 + H2O" attributes:attr];

[self attributedString:s setSuperscriptInRange:NSMakeRange(6,1)];
[self attributedString:s setSubscriptInRange:NSMakeRange(11,1)];

[[txtView textStorage] setAttributedString: s];
[s release];

// **** Quit btn **** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 60, 10, 45, 45 )];
 [quitBtn setBezelStyle:NSCircularBezelStyle ];
 [quitBtn setTitle: @"Q" ];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
 [quitBtn release];
}
@end
EndC

BeginCCode
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSApplication *application = [NSApplication sharedApplication];
MainWindow *mainWindow = [[MainWindow alloc]init];
[mainWindow buildWnd];
[application run];
[mainWindow release];
[pool drain];
EndC