OS X Core Controls Tutorial: Part 1/2 学习笔记

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

NSPopupButton_from_raywenderlich

// 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];

NSMutableAttriburtedString

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.

图片来自raywenderlich

@property (copy) NSString *title;
@property (getter=isEnabled) BOOL enabled;// ps:NSControl
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值