NSButton how to color the text

后端 未结 12 1494
伪装坚强ぢ
伪装坚强ぢ 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:11

    Here is two other solutions: http://denis-druz.okis.ru/news.534557.Text-Color-in-NSButton.html

    solution 1:

    -(void)awakeFromNib
    {
        NSColor *color = [NSColor greenColor];
        NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
        NSRange titleRange = NSMakeRange(0, [colorTitle length]);
        [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:titleRange];
        [button setAttributedTitle:colorTitle];
    }
    

    solution 2:

    in *.m file:

    - (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title withColor:(NSColor*)color
    {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        [style setAlignment:NSCenterTextAlignment];
        NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil];
        NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:title attributes:attrsDictionary];
        [button setAttributedTitle:attrString];
    }
    
    -(void)awakeFromNib
    {
        NSString *title = @"+Add page";
        NSColor *color = [NSColor greenColor];
        [self setButtonTitleFor:button toString:title withColor:color];
    }
    
    0 讨论(0)
  • 2020-12-13 01:11

    Apple have code for setting the text colour of an NSButton as part of the Popover example.

    Below is the crux of the example (modified slightly for this post, untested):

    NSButton *button = ...;
    NSMutableAttributedString *attrTitle =
        [[NSMutableAttributedString alloc] initWithString:@"Make Me Red"];
    NSUInteger len = [attrTitle length];
    NSRange range = NSMakeRange(0, len);
    [attrTitle addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:range];
    [attrTitle fixAttributesInRange:range];
    [button setAttributedTitle:attrTitle];
    

    Note that the call to fixAttributesInRange: seems to be important (an AppKit extension), but I can't find documentation as to why that is the case. The only concern I have with using attributed strings in an NSButton is if an image is also defined for the button (such as an icon), the attributed string will occupy a large rectangle and push the image to the edge of the button. Something to bear in mind.

    Otherwise it seems the best way is to make your own drawRect: override instead, which has many other pitfalls that are outside the scope of this question.

    0 讨论(0)
  • 2020-12-13 01:13

    Add a category on the NSButton and simply set the color to what you want, and preseve the existing attributes, since the title can be centered, left aligned etc


    @implementation NSButton (NSButton_IDDAppKit)
    
    - (NSColor*)titleTextColor {
    
        return [NSColor redColor];
    
    }
    
    - (void)setTitleTextColor:(NSColor*)aColor {
    
        NSMutableAttributedString*  attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedTitle];
        NSString*  title = self.title;
        NSRange  range = NSMakeRange(0.0, self.title.length);
    
        [attributedString addAttribute:NSForegroundColorAttributeName value:aColor range:range];
        [self setAttributedTitle:attributedString];
        [attributedString release];
    
    }
    
    @end
    
    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
  • 2020-12-13 01:16

    Swift 5

    You can use this extension:

    extension NSButton {
    
    @IBInspectable var titleColor: NSColor? {
        get {
            return   NSColor.white
        }
        set {
            let pstyle = NSMutableParagraphStyle()
            pstyle.alignment = .center
            
            self.attributedTitle = NSAttributedString(
                string: self.title,
                attributes: [ NSAttributedString.Key.foregroundColor :newValue, NSAttributedString.Key.paragraphStyle: pstyle])
            
        }
       }
     }
    

    Now you can set title color in storyboard easily. Cheers!

    0 讨论(0)
  • 2020-12-13 01:22

    When your target is macOS 10.14 or newer, you can use the new contentTintColor property of the NSButton control to set the text color.

    button.contentTintColor = NSColor.systemGreenColor;
    
    0 讨论(0)
提交回复
热议问题