NSButton how to color the text

后端 未结 12 1527
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 00:35

on OSX I have an NSButton with a pretty dark image and unfortunately it is not possible to change the color using the attributes inspector. See picture the big black button,

12条回答
  •  臣服心动
    2020-12-13 01:13

    A really simple, reusable solution without subclassing NSButton:

    [self setButton:self.myButton fontColor:[NSColor whiteColor]] ;
    
    -(void) setButton:(NSButton *)button fontColor:(NSColor *)color {
        NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
        [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, button.attributedTitle.length)];
        [button setAttributedTitle:colorTitle];
    }
    

提交回复
热议问题