https://www.jianshu.com/p/9299e2b03330
2017.11.17
以下記錄關於按鈕NSTextField在項目中涉及到的需求:
1、取消焦點的高亮狀態:
//點擊的時候不顯示藍色外框 self.focusRingType = NSFocusRingTypeNone;
2、文字垂直居中:
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame { // super would normally draw text at the top of the cell CGFloat fontSize = self.font.boundingRectForFont.size.height; NSInteger offset = floor((NSHeight(frame) - ceilf(fontSize))/2)-5; NSRect centeredRect = NSInsetRect(frame, 0, offset); return centeredRect; } - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event { [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate event:event]; } - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate start:(NSInteger)start length:(NSInteger)length { [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate start:start length:length]; } - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view { [super drawInteriorWithFrame: [self adjustedFrameToVerticallyCenterText:frame] inView:view]; }
3、文字內容富文本顯示:
NSString *string = @"這是藍色文字,這是紅色文字。"; NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithString: string]; [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:NSMakeRange(0, 7)]; [colorTitle addAttribute:NSForegroundColorAttributeName value:HEX_RGB_COLOR(0x38b162) range:NSMakeRange(7, 7)]; self.attributedStringValue = colorTitle;
4、改變邊框顏色:
self.bordered = YES; self.wantsLayer = YES; self.layer.borderColor = [NSColor redColor].CGColor; self.layer.borderWidth = 1.0f; // 重要:一定要設置如下屬性,否則無法顯示效果 [[self cell] setBezeled:NO]; [[self cell] setBordered:NO];
5、多行文字換行:

如何換行
題外話:整個窗體失去焦點,不閃爍光標:[self.window makeFirstResponder:nil];
作者:背靠背的微笑
鏈接:https://www.jianshu.com/p/9299e2b03330
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。
- 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坐標體系中,原點在左下角,這和數學中的坐標系一致。運行工程,效果如下圖所示:
- 阿里雲 > 教程中心 > ios教程 > NSTextField控件應用詳解
- NSTextField控件應用詳解
-
發布時間:2018-02-08 來源:網絡 上傳者:用戶
關鍵字: 控件 NSTextField 詳解 應用
- 摘要: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坐標體系中,原點在左下角,這和數學中的坐標系一致。運行工程,效果如下圖所示:
-
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控件在活動過程中的回調方法,例如開始編輯,結束編輯等。
以上是
-
的內容,更多
-
的內容,請您使用右上方搜索功能獲取相關信息。