美文网首页
NSButton 设置背景颜色

NSButton 设置背景颜色

作者: 大漠赏花 | 来源:发表于2018-01-06 18:46 被阅读0次

NSButton 不像 UIButton 那样可以直接设置颜色,需要自定义一个NSButton类,重写drawRect:方法

- (void)drawRect:(NSRect)dirtyRect 

    [super drawRect:dirtyRect]; 

    //背景颜色 

    [[NSColor blueColor] set]; 

    NSRectFill(self.bounds); 

    //绘制文字 

    if (titleString != nil) { 

        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init]; 

        [paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]]; 

        [paraStyle setAlignment:NSCenterTextAlignment]; 

        //[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail]; 

        NSDictionary *attrButton = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Verdana" size:14], NSFontAttributeName, [NSColor colorWithCalibratedRed:255 green:255 blue:255 alpha:1], NSForegroundColorAttributeName, paraStyle, NSParagraphStyleAttributeName, nil]; 

        NSAttributedString * btnString = [[NSAttributedString alloc] initWithString:titleString attributes:attrButton]; 

        [btnString drawInRect:NSMakeRect(0, 4, self.frame.size.width, self.frame.size.height)]; 

    } 

}

注:不绘制文字的话文字就不会显示

参考资料:http://www.cocoachina.com/bbs/read.php?tid=271752

相关文章

  • NSButton 设置背景颜色

    NSButton 不像 UIButton 那样可以直接设置颜色,需要自定义一个NSButton类,重写drawRe...

  • Mac开发-NSButton

    NSButton添加下划线 设置下划线颜色

  • NSButton设置字体颜色

    众所周知NSButton不同于UIButton,哪怕是想设置一下字体颜色通常都要写一个新类继承NSButton,然...

  • MacOS开发NSButton问题总结

    NSButton设置高度无效解决办法: 然后在自定义NSButton中设置其frame。 NSButton在Xib...

  • Mac osx开发 NSButton 修改背景色,字体颜色,设置

    1.设置NSButton的背景色 _btn.wantsLayer = YES; _btn.layer.backgr...

  • Mac开发 NSButton 字体颜色,

    初始化NSbutton(这里就不做布局设置了) NSButton *btn = [[NSButton alloc]...

  • 元素操作

    设置元素的背景: 使用background-color属性设置元素背景颜色 如果不设置背景颜色,元素默认背景颜色为...

  • CSS背景和精灵图

    1 背景颜色 1 如何设置标签的背景颜色2 设置父元素背景颜色会不会影响子元素背景颜色 2 背景图片 1 如何设置...

  • 设置元素背景

    设置元素的背景 设置元素的背景,有背景颜色和背景图片。设置元素的背景颜色 我们在之前的文章中都一直在用设置背景颜色...

  • 设置背景颜色

    两种方法实现Activity透明/半透明效果的设置,代码思路很有调理,感兴趣的朋友可以参考下,希望本文可以帮助到你...

网友评论

      本文标题:NSButton 设置背景颜色

      本文链接:https://www.haomeiwen.com/subject/zkzynxtx.html