Created
March 6, 2019 19:18
-
-
Save itfrombit/08660a51e5c4ac4f7037f22af2ddf2d3 to your computer and use it in GitHub Desktop.
Programmatically creating an NSTextField with label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSTextField* xxxUIControlCreateTextField(NSStackView* parent, const char* label, const char* default_value, u32 tag, float preferred_width, float view_width) | |
{ | |
NSStackView* stack = [[NSStackView alloc] init]; | |
stack.translatesAutoresizingMaskIntoConstraints = NO; | |
stack.orientation = NSUserInterfaceLayoutOrientationHorizontal; | |
stack.spacing = 4; | |
NSTextField* tf_label = nil; | |
if (label) | |
{ | |
tf_label = [NSTextField labelWithString:[NSString stringWithUTF8String:label]]; | |
tf_label.tag = -1; | |
tf_label.editable = NO; | |
tf_label.alignment = NSTextAlignmentRight; | |
tf_label.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; | |
xxxUIControlMetrics m = xxxUIControlMetricsList[UIControlLabel]; | |
//NSRect frame = NSMakeRect(m.left_edge, *view_start_y + 2, m.right_edge - m.left_edge, m.height); | |
NSRect frame = NSMakeRect(0.0f, 0.0f, m.right_edge - m.left_edge, m.height); | |
tf_label.frame = frame; | |
[stack addView:tf_label inGravity:NSStackViewGravityLeading]; | |
//[parent addSubview:tf]; | |
} | |
NSString* defaultString = @""; | |
if (default_value) | |
{ | |
defaultString = [NSString stringWithUTF8String:default_value]; | |
} | |
NSTextField* tf = [NSTextField textFieldWithString:defaultString]; | |
tf.tag = tag; | |
tf.editable = YES; | |
tf.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; | |
UIControlMetrics m = xxxUIControlMetricsList[xxxUIControlTextField]; | |
float width = (preferred_width > 0.0f) ? preferred_width : view_width - m.left_edge - SKY_UI_CONTROL_RIGHT_MARGIN; | |
NSRect frame = NSMakeRect(0, 0, width, m.height); | |
tf.frame = frame; | |
[stack addView:tf inGravity:NSStackViewGravityLeading]; | |
NSDictionary* views; | |
if (label) | |
{ | |
views = @{@"label": tf_label, @"tf": tf}; | |
[parent addConstraints: | |
[NSLayoutConstraint constraintsWithVisualFormat: @"H:|-[label(==60)]-[tf]-|" | |
options: 0 | |
metrics: nil | |
views: views]]; | |
} | |
else | |
{ | |
views = @{@"tf": tf}; | |
[parent addConstraints: | |
[NSLayoutConstraint constraintsWithVisualFormat: @"H:|-[tf]-|" | |
options: 0 | |
metrics: nil | |
views: views]]; | |
} | |
[parent addView:stack inGravity:NSStackViewGravityTop]; | |
return tf; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment