oc之NSTextField控件應用詳解


https://www.aliyun.com/jiaocheng/358146.html

摘要:NSTextField控件應用詳解NSTextField用來接收用戶文本輸入,其可以接收鍵盤事件。創建NSTextFiled的示例代碼如下:-(void)viewDidLoad{[superviewDidLoad];//創建TextField對象_textField=[[NSTextFieldalloc]initWithFrame:NSMakeRect(50,30,200,50)];//設置默認顯示的提示字符串_textField.placeholderString=@&qu

NSTextField控件應用詳解 

NSTextField用來接收用戶文本輸入,其可以接收鍵盤事件。創建NSTextFiled的示例代碼如下:

- (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坐標體系中,原點在左下角,這和數學中的坐標系一致。運行工程,效果如下圖所示:

pastedGraphic.png

NSTextField類中常用的屬性和方法列舉如下:

//設置默認顯示的提示文字 

@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 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控件在活動過程中的回調方法,例如開始編輯,結束編輯等。

以上是NSTextField控件應用詳解的內容,更多 控件 NSTextField 詳解 應用 的內容,請您使用右上方搜索功能獲取相關信息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM