NSTextField
是一个功能强大的控件,不仅可以作为简单的文本输入框,还可以实现更多高级功能。例如,支持富文本、实现自定义绘制、处理复杂的输入校验等。
进阶使用和技巧
1. 富文本显示与编辑
NSTextField
支持富文本,也就是说你可以为文本设置不同的颜色、字体、大小等。
设置富文本
Objective-C
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Hello, World!"
attributes:@{
NSForegroundColorAttributeName: [NSColor blueColor],
NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:16]
}];
[self.textField setAttributedStringValue:attrString];
Swift
let attrString = NSAttributedString(string: "Hello, World!", attributes: [
.foregroundColor: NSColor.blue,
.font: NSFont(name: "Helvetica-Bold", size: 16)!
])
textField.attributedStringValue = attrString
2. 自定义绘制文本字段
可以通过子类化 NSTextField
实现自定义的绘制逻辑,以实现独特的 UI 效果。
自定义绘制
Objective-C
@implementation CustomTextField
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// 自定义绘制背景
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
// 自定义绘制文本
NSDictionary *attributes = @{
NSFontAttributeName: [NSFont systemFontOfSize:14],
NSForegroundColorAttributeName: [NSColor blackColor]
};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.stringValue attributes:attributes];
[attrString drawInRect:dirtyRect];
}
@end
Swift
class CustomTextField: NSTextField {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// 自定义绘制背景
NSColor.white.setFill()
dirtyRect.fill()
// 自定义绘制文本
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: 14),
.foregroundColor: NSColor.black
]
let attrString = NSAttributedString(string: self.stringValue, attributes: attributes)
attrString.draw(in: dirtyRect)
}
}
3. 定制化输入校验与过滤
可以使用 NSTextFieldDelegate
来实现定制化的输入校验和过滤功能。
实现输入过滤
Objective-C
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
if (commandSelector == @selector(insertNewline:)) {
NSString *inputText = [textView string];
// 自定义校验逻辑
if ([self isValidInput:inputText]) {
return NO; // 允许插入新行
} else {
return YES; // 禁止插入新行
}
}
return NO;
}
Swift
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if commandSelector == #selector(insertNewline(_:)) {
let inputText = textView.string
// 自定义校验逻辑
if isValidInput(inputText) {
return false // 允许插入新行
} else {
return true // 禁止插入新行
}
}
return false
}
4. 处理剪切板操作
可以拦截并处理剪切板(拷贝、剪切、粘贴)操作,从而实现更多控制。
自定义剪切板操作处理
Objective-C
- (void)paste:(id)sender {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *pasteString = [pasteboard stringForType:NSPasteboardTypeString];
// 自定义粘贴逻辑
if ([self isValidInput:pasteString]) {
[self setStringValue:pasteString];
} else {
NSBeep(); // 发出警告声
}
}
Swift
override func paste(_ sender: Any?) {
let pasteboard = NSPasteboard.general
if let pasteString = pasteboard.string(forType: .string) {
// 自定义粘贴逻辑
if isValidInput(pasteString) {
self.stringValue = pasteString
} else {
NSBeep() // 发出警告声
}
}
}
5. 自定义工具类封装
封装一个 NSTextField
的工具类,提供多个接口供外部使用,可灵活定制。
Objective-C
@interface NSTextFieldCustomizer : NSObject
@property (nonatomic, strong) NSTextField *textField;
- (instancetype)initWithTextField:(NSTextField *)textField;
- (void)setPlaceholder:(NSString *)placeholder;
- (void)setTextColor:(NSColor *)color;
- (void)setFont:(NSFont *)font;
- (void)setAlignment:(NSTextAlignment)alignment;
- (void)setEditable:(BOOL)editable;
- (void)setBordered:(BOOL)bordered;
- (void)setBackgroundColor:(NSColor *)color;
@end
@implementation NSTextFieldCustomizer
- (instancetype)initWithTextField:(NSTextField *)textField {
self = [super init];
if (self) {
_textField = textField;
}
return self;
}
- (void)setPlaceholder:(NSString *)placeholder {
[self.textField setPlaceholderString:placeholder];
}
- (void)setTextColor:(NSColor *)color {
[self.textField setTextColor:color];
}
- (void)setFont:(NSFont *)font {
[self.textField setFont:font];
}
- (void)setAlignment:(NSTextAlignment)alignment {
[self.textField setAlignment:alignment];
}
- (void)setEditable:(BOOL)editable {
[self.textField setEditable:editable];
}
- (void)setBordered:(BOOL)bordered {
[self.textField setBordered:bordered];
}
- (void)setBackgroundColor:(NSColor *)color {
[self.textField setBackgroundColor:color];
}
@end
Swift
class NSTextFieldCustomizer {
private weak var textField: NSTextField?
init(textField: NSTextField) {
self.textField = textField
}
func setPlaceholder(_ placeholder: String) {
textField?.placeholderString = placeholder
}
func setTextColor(_ color: NSColor) {
textField?.textColor = color
}
func setFont(_ font: NSFont) {
textField?.font = font
}
func setAlignment(_ alignment: NSTextAlignment) {
textField?.alignment = alignment
}
func setEditable(_ editable: Bool) {
textField?.isEditable = editable
}
func setBordered(_ bordered: Bool) {
textField?.isBordered = bordered
}
func setBackgroundColor(_ color: NSColor) {
textField?.backgroundColor = color
}
}
使用示例
Objective-C
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 22)];
NSTextFieldCustomizer *customizer = [[NSTextFieldCustomizer alloc] initWithTextField:textField];
[customizer setPlaceholder:@"Enter your name"];
[customizer setTextColor:[NSColor blueColor]];
[customizer setFont:[NSFont fontWithName:@"Helvetica" size:14]];
[customizer setAlignment:NSTextAlignmentCenter];
[customizer setEditable:YES];
[customizer setBordered:YES];
[customizer setBackgroundColor:[NSColor whiteColor]];
Swift
let textField = NSTextField(frame: NSMakeRect(0, 0, 200, 22))
let customizer = NSTextFieldCustomizer(textField: textField)
customizer.setPlaceholder("Enter your name")
customizer.setTextColor(.blue)
customizer.setFont(NSFont(name: "Helvetica", size: 14)!)
customizer.setAlignment(.center)
customizer.setEditable(true)
customizer.setBordered(true)
customizer.setBackgroundColor(.white)
标签:12,自定义,void,NSTextField,Mac,textField,customizer,self
From: https://www.cnblogs.com/chglog/p/18345386