美文网首页
Mac osx开发 NSButton 修改背景色,字体颜色,设置

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

作者: fancy啊fancy | 来源:发表于2018-01-25 19:06 被阅读0次

1.设置NSButton的背景色

_btn.wantsLayer = YES;

 _btn.layer.backgroundColor = [NSColor blueColor].CGColor;

2.设置NSButton文字和字体颜色(应该还有其他方法修改按钮的字体颜色,这里只是参考)

[self setBtnTitleColorwithColor:[NSColor whiteColor] andStr:@"Login" andBtn:_btn];

-(void)setBtnTitleColorwithColor:(NSColor*)color andStr:(NSString*)str andBtn:(NSButton*)btn{

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

    pghStyle.alignment = NSTextAlignmentCenter;

    // 创建Attributes,设置颜色和段落样式

    NSDictionary *dicAtt = @{NSForegroundColorAttributeName:color, NSParagraphStyleAttributeName: pghStyle};

    btn.title=@" ";

    NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc] initWithAttributedString:btn.attributedTitle];

    // 替换文字

    [attTitlereplaceCharactersInRange:NSMakeRange(0, 1) withString:str];

    // 添加属性

    [attTitleaddAttributes:dicAtt range:NSMakeRange(0, str.length)];

    btn.attributedTitle= attTitle;

}

3.设置NSButton圆角

    [_btn.layer setCornerRadius:16];

    [_btn.layer setMasksToBounds:YES];

相关文章

网友评论

      本文标题:Mac osx开发 NSButton 修改背景色,字体颜色,设置

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