[Obj-C] 04 GUIアプリ : NSTextField

[M1 Mac, Big Sur 11.6.5, no Xcode]

テキストラベルを配置しました。書き方自体はSwiftやFLTKと似ているため、角括弧への違和感が取れないながらも少しずつ慣れてきました。

Windowや図形のY座標は、ディスプレイやWindowの縦サイズから図形の高さと左上基準のY座標を引く必要がありややこしいです。

あとはテキストエリアとボタン類なのでGUIについてはスムーズに実装できそうです。

#include <Cocoa/Cocoa.h>

@interface ConvertorWindow : NSWindow {
  NSTextField* label1;
  NSTextField* label2;
}
- (instancetype)init;
- (BOOL)windowShouldClose:(id)sender;
@end

@implementation ConvertorWindow
- (instancetype)init {
	NSColor *background = [NSColor colorWithCalibratedRed:(double)247/255 green:(double)252/255 blue:(double)254/255 alpha:1.0f];
	NSColor *foreground = [NSColor colorWithCalibratedRed:(double)57/255 green:(double)63/255 blue:(double)76/255 alpha:1.0f];

	label1 = [[[NSTextField alloc] initWithFrame:NSMakeRect(10, 265-16-15, 34, 16)] autorelease];
	[label1 setFont:[NSFont fontWithName:@"Arial" size:14]];
	[label1 setStringValue:@"FILE"];
	[label1 setBezeled:NO];
	[label1 setDrawsBackground:NO];
	[label1 setEditable:NO];
	[label1 setSelectable:NO];
	[label1 setTextColor:foreground];

	label2 = [[[NSTextField alloc] initWithFrame:NSMakeRect(10, 265-16-45, 34, 16)] autorelease];
	[label2 setFont:[NSFont fontWithName:@"Arial" size:14]];
	[label2 setStringValue:@"LIST"];
	[label2 setBezeled:NO];
	[label2 setDrawsBackground:NO];
	[label2 setEditable:NO];
	[label2 setSelectable:NO];
	[label2 setTextColor:foreground];

	[super initWithContentRect:NSMakeRect(100, 1080-265-100, 360, 265) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
	[self setTitle:@"Xlsx Convertor"];
	[[self contentView] addSubview:label1];
	[[self contentView] addSubview:label2];
	[self setIsVisible:YES];
	[self setBackgroundColor:background];
	return self;
}

- (BOOL)windowShouldClose:(id)sender {
  [NSApp terminate:sender];
  return YES;
}
@end

int main(int argc, char* argv[]) {
  [NSApplication sharedApplication];
  [[[[ConvertorWindow alloc] init] autorelease] makeMainWindow];
  [NSApp run];
}