MAC开发--NSTextField汇总


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控件应用详解_IOS

 
    • 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