NSTextField控件應用詳解

NSTextField控件應用詳解

    NSTextField用來接收用戶文本輸入,其能夠接收鍵盤事件。建立NSTextFiled的示例代碼以下:app

- (void)viewDidLoad {
    [super viewDidLoad];
    //建立TextField對象
    _textField = [[NSTextField alloc]initWithFrame:NSMakeRect(50, 30, 200, 50)];
    //設置默認顯示的提示字符串
    _textField.placeholderString = @"請填寫你的夢想";
    //設置默認顯示的提示字符串 使用的帶屬性的字符串
    NSMutableAttributedString * attriString = [[NSMutableAttributedString alloc]initWithString:@"請填寫你的夢想"];
    [attriString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(5, 2)];
    _textField.placeholderAttributedString = attriString;
    //設置文本框背景顏色
    _textField.backgroundColor = [NSColor greenColor];
    //設置是否繪製背景
    _textField.drawsBackground = YES;
    //設置文字顏色
    _textField.textColor = [NSColor blueColor];
    //設置是否顯示邊框
    _textField.bordered = YES;
    //設置是否繪製貝塞爾風格的邊框
    _textField.bezeled = YES;
    //設置是否能夠編輯
    _textField.editable = YES;
    //設置文本框是否能夠選中
    _textField.selectable = YES;
    //設置貝塞爾風格
    _textField.bezelStyle = NSTextFieldSquareBezel;
    //設置傾向佈局寬度
    _textField.preferredMaxLayoutWidth = 100;
    //設置最大行數
    _textField.maximumNumberOfLines = 5;
    //設置斷行模式
    [[_textField cell] setLineBreakMode:NSLineBreakByCharWrapping];
    //設置是否啓用單行模式
    [[_textField cell]setUsesSingleLineMode:NO];
    //設置超出行數是否隱藏
    [[_textField cell] setTruncatesLastVisibleLine: YES ];
    [self.view addSubview:_textField];
}

須要注意,在AppKit座標體系中,原點在左下角,這和數學中的座標系一致。運行工程,效果以下圖所示:佈局

NSTextField類中經常使用的屬性和方法列舉以下:spa

//設置默認顯示的提示文字
@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;
//設置代理
@property (nullable, assign) id<NSTextFieldDelegate> delegate;
//獲取是否接受第一響應
@property (readonly) BOOL acceptsFirstResponder;
//設置貝塞爾風格
/*
typedef NS_ENUM(NSUInteger, NSTextFieldBezelStyle) {
    NSTextFieldSquareBezel  = 0,
    NSTextFieldRoundedBezel = 1
};
*/
@property NSTextFieldBezelStyle bezelStyle;
//設置一個預約的最大寬度
@property CGFloat preferredMaxLayoutWidth;
//設置最大行數
@property NSInteger maximumNumberOfLines;
//設置是否容許編輯文本屬性
@property BOOL allowsEditingTextAttributes;
//設置是否容許用戶向文本框中拖拽圖片
@property BOOL importsGraphics;

//下面這些方法用於子類進行重寫
//選擇文本框時調用
- (void)selectText:(nullable id)sender;
//詢問是否容許開始編輯文本框
- (BOOL)textShouldBeginEditing:(NSText *)textObject;
//詢問是否容許結束編輯文本框
- (BOOL)textShouldEndEditing:(NSText *)textObject;
//文本框已經開始進入編輯的通知
- (void)textDidBeginEditing:(NSNotification *)notification;
//文本框已經結束編輯的通知
- (void)textDidEndEditing:(NSNotification *)notification;
//文本框中文字發生變化的通知
- (void)textDidChange:(NSNotification *)notification;

//下面兩個屬性與TouchBar相關 只有再較高版本的mac電腦中有效
//自動完成編輯
@property (getter=isAutomaticTextCompletionEnabled) BOOL automaticTextCompletionEnabled NS_AVAILABLE_MAC(10_12_2);
//字符選擇按鈕
@property BOOL allowsCharacterPickerTouchBarItem NS_AVAILABLE_MAC(10_12_2);

//下面是一些便捷建立NSTextField對象的方法
+ (instancetype)labelWithString:(NSString *)stringValue NS_SWIFT_NAME(init(labelWithString:)) NS_AVAILABLE_MAC(10_12);
+ (instancetype)wrappingLabelWithString:(NSString *)stringValue NS_SWIFT_NAME(init(wrappingLabelWithString:)) NS_AVAILABLE_MAC(10_12);
+ (instancetype)labelWithAttributedString:(NSAttributedString *)attributedStringValue NS_SWIFT_NAME(init(labelWithAttributedString:)) NS_AVAILABLE_MAC(10_12);
+ (instancetype)textFieldWithString:(nullable NSString *)stringValue NS_AVAILABLE_MAC(10_12);

NSTextField類繼承自NSControl類,NSControl類中定義了許多屬性能夠獲取到文本框中的文本,例如stringValue屬性,本文中再也不贅述。代理

    關於NSTextFieldDelegate協議,其其實是繼承自NSControlTextEditingDelegate協議,這個協議中定義了NSTextField控件在活動過程當中的回調方法,例如開始編輯,結束編輯等。code

相關文章
相關標籤/搜索