对于大部分iOS开发者而言UITextField并不陌生,然而maccocoa开发接触的更底层的cocoa框架,相对于iOS框架更加完善和可扩展性更高,但相对来说增加了难度.今天就大致讲一下NSTextField一些常见的使用技巧.
分别为:NSTextField和NSSecureTextField
1. NSTextField和NSSecureTextField的自定义
@property (nullable, copy) NSString *placeholderString NS_AVAILABLE_MAC(10_10);
@property (nullable, copy) NSAttributedString *placeholderAttributedString NS_AVAILABLE_MAC(10_10);
@property (nullable, copy) NSColor *backgroundColor;
@property BOOL drawsBackground;
@property (nullable, copy) NSColor *textColor;
@property (getter=isBordered) BOOL bordered;
@property (getter=isBezeled) BOOL bezeled;
@property (getter=isEditable) BOOL editable;
@property (getter=isSelectable) BOOL selectable;
- (void)selectText:(nullable id)sender;
@property (nullable, weak) id<NSTextFieldDelegate> delegate;
- (BOOL)textShouldBeginEditing:(NSText *)textObject;
- (BOOL)textShouldEndEditing:(NSText *)textObject;
- (void)textDidBeginEditing:(NSNotification *)notification;
- (void)textDidEndEditing:(NSNotification *)notification;
- (void)textDidChange:(NSNotification *)notification;
@property (readonly) BOOL acceptsFirstResponder;
@property NSTextFieldBezelStyle bezelStyle;
上面API写的很清楚,如果不理解的可以下面回复询问.
主要是利用上面的属性做到一些自定义控件.如下:
- (instancetype)init {
self = [super init];
if (self) {
self.bezelStyle = NSTextFieldRoundedBezel;
self.bordered = NO;
self.focusRingType = NSFocusRingTypeNone;
self.font = [NSFont systemFontOfSize:15.0f];
self.delegate =self;
}
return self;
}
// 为文本增加下划线
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSRect rect = CGRectMake(0, dirtyRect.size.height-10, dirtyRect.size.width, 1);
NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
[EC_RGB_String(@"#d9dcde") set];
[path fill];
}
// 占位文本的清除
- (BOOL)becomeFirstResponder {
if (self.placeholderString.length>0 && EC_MAC_OS_X_10_10) {
self.placeStr = self.placeholderString;
self.placeholderString = @"";
}
return [super becomeFirstResponder];
}
// 以下可以自定义编写一些定时化的需求. NSNotification的对象是NSTextView.
- (BOOL)textShouldBeginEditing:(NSText *)textObject;
- (BOOL)textShouldEndEditing:(NSText *)textObject;
- (void)textDidBeginEditing:(NSNotification *)notification;
- (void)textDidEndEditing:(NSNotification *)notification;
- (void)textDidChange:(NSNotification *)notification;
2. NSTextField和NSSecureTextField接收键盘控制以及控制文本的长度.
// 尽量让controller作为TextField的代理.
#pragma mark - NSTextFieldDelegate
-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
if (commandSelector == @selector(insertNewline:)) {// 监听键盘的回车事件.
[self loginWithAccount:_accountF.stringValue pwd:_pwdF.stringValue];
return true;
}
return false;
}
// 控制文本输入框的内容长度.
-(void)controlTextDidChange:(NSNotification *)obj {
NSInteger accountMaxLimit = 18;
if (self.accountF.stringValue.length > accountMaxLimit && obj.object == self.accountF)
self.accountF.stringValue = [self.accountF.stringValue substringToIndex:accountMaxLimit];
if (self.pwdF.stringValue.length > accountMaxLimit && obj.object == self.pwdF)
self.pwdF.stringValue = [self.pwdF.stringValue substringToIndex:accountMaxLimit];
}
3. 文本框默认选中状态.
类似mac电脑上qq客户端登陆页面账号文本框是选中状态,方便用户回车登录.
// 设置以下属性值可以让文本框默认选中状态.
- (void)viewDidAppear {
[super viewDidAppear];
[self.accountF setSelectable:YES];
[self.accountF selectText:self];
}
特别提醒
NSSecureTextField控件控制输入内容是暗文的控件.
网友评论