learn from: OS X Core Controls Tutorial: Part 1/2
Introduce
one big difference with Mac Development are there are diffferent controls.
have a foundamental understanding of the following controls:
Labels and TextField, Popup Buttons, Text Views, Sliders, Date pickers ,Buttons, Radio buttons, check Buttons, image View.
NSControl
NSControl provides three features: drawing controls on the screen, responding to user events, sending action messages.
Setting The Control’s Value
@property (copy) NSString *stringValue;
@property (copy) NSAttributedString *attributedStringValue;
@property (nullable, copy) id /* id<NSCopying> */ objectValue;
@property int intValue;
@property NSInteger integerValue;
@property float floatValue;
@property double doubleValue;
Enabling & Disabling a Control
@property (getter=isEnabled) BOOL enabled;
NSTextField
one of the most common controls in any UI is a field that can be used to display or edit text.
if you want a textfield to be a label, simply set it as a non-editable.
@property (getter=isEditable) BOOL editable;
NSComboBox
it allows the user to choose one value from an array of options, as well as enter their own text. Example in OS X: Date & time preferences panel
NSComboBox has two distinct components: the textField can be type, the list of options which appear when the embedded button is clicked.
M1–Calling methods Directory on the control
- (void)selectItemAtIndex:(NSInteger)index;
- (void)deselectItemAtIndex:(NSInteger)index;
@property (readonly) NSInteger indexOfSelectedItem;
/* These methods can only be used when usesDataSource is NO */
- (void)addItemWithObjectValue:(id)object;
- (void)addItemsWithObjectValues:(NSArray *)objects;
- (void)insertItemWithObjectValue:(id)object atIndex:(NSInteger)index;
- (void)removeItemWithObjectValue:(id)object;
- (void)removeItemAtIndex:(NSInteger)index;
- (void)removeAllItems;
- (void)selectItemWithObjectValue:(nullable id)object;
- (id)itemObjectValueAtIndex:(NSInteger)index;
M2- Using a Data Source
NSComboBoxDataSource,NSComboBoxDelegate。
1. set control’s datasource
myComboBox.dataSource = self;
myComboBox.useDataSource = true;
2. implement datasource
@protocol NSComboBoxDataSource <NSObject>
@optional
/* These two methods are required when not using bindings */
-(NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
-(id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
3. reload the data in a combo box
- (void)reloadData;
NSPopupButton
The control allows user to choose from an array of options,it’s incredibly common in OS X
// Adding and removing items
- (void)addItemWithTitle:(NSString *)title;
- (void)addItemsWithTitles:(NSArray<NSString *> *)itemTitles;
- (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index;
- (void)removeItemWithTitle:(NSString *)title;
- (void)removeItemAtIndex:(NSInteger)index;
- (void)removeAllItems;
@property (readonly) NSInteger indexOfSelectedItem;
// Dealing with selection
- (void)selectItem:(nullable NSMenuItem *)item;
- (void)selectItemAtIndex:(NSInteger)index;
- (void)selectItemWithTitle:(NSString *)title;
- (BOOL)selectItemWithTag:(NSInteger)tag;
NSTextView
usually for displaying rich text.Some implementations even allow for more advanced features such as displaying inline images.contain scroll views
some basic methods
@property (nullable, copy) NSString *string;// ps: NSText
@property (nullable, copy) NSColor *textColor; // Default is nil. If nil, draws with blackColor ps: NSText
@property (getter=isEditable) BOOL editable;
@property (copy) NSColor *backgroundColor;
attributed Strings
NSTextView has built-in support for NSAttributedString.
@property (nullable, readonly, assign) NSTextStorage *textStorage;
demo
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:@"Red Green"];
[myAttributedString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, 3)];
[myAttributedString addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:NSMakeRange(4, 5)];
[self.phraseTextView.textStorage setAttributedString:myAttributedString];
NSButton
on OS X there are many different styles of buttons.There all work in much the same way,the only difference being their visual representation.
@property (copy) NSString *title;
@property (getter=isEnabled) BOOL enabled;// ps:NSControl