2016-12-30 52 views
0

我正在使用XIB切換按鈕,NSButton切換:的OnClick標題得到<strong>AwakeFromNib</strong>在OSX 10.11

刪除:我設置標題和按鈕的背景圖片。 代碼工作得很好,直到OSX 10.10.5及以下更高版本當我點擊按鈕的文字獲取刪除

集歸因標題

[self.deleteButton setWantsLayer:YES]; 
    self.deleteButton.layer.backgroundColor = [self setButtonBGColor]; 
    [self setButtonTitleFor:self.deleteButton 
        toString:@"Delete"]; 
    [self.deleteButton setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawOnSetNeedsDisplay]; 



- (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title 
{ 
    NSAttributedString *attrString = [self getAttributedStringForString:title]; 
    [button setAttributedTitle:attrString]; 
} 

enter image description here enter image description here

任何想法的帽子應該做的。

enter image description here

+0

任何人都可以提供一些幫助嗎? – Swati

回答

0

因此終於得到了它的權利

子類NSButtonCell幫了我

RPButtonTextTopCell.h

@interface RPButtonTextTopCell:NSButtonCell

@end

RPButtonTextTopCell.m

@implementation RPButtonTextTopCell

-(id) init 
{ 
    self = [super init]; 
    if (self) { 
    } 
    return self; 
} 

-(id) initWithCoder:(NSCoder *)decoder 
{ 
    return [super initWithCoder:decoder]; 
} 

-(id) initTextCell:(NSString *)string 
{ 
    return [super initTextCell:string]; 
} 

-(NSRect)titleRectForBounds:(NSRect)theRect 
{ 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 

    titleFrame.origin.y = (theRect.origin.y - (theRect.size.height-titleSize.height)*0.5) + 5; 

    return titleFrame; 
} 

@end

利用這種定製類

RPButtonTextTopCell *deleteCell = [[RPButtonTextTopCell alloc] init]; 
deleteCell.backgroundColor  = [self setNSButtonBGColor]; 

[self.deleteButton setCell:deleteCell]; 
[self setButtonTitleFor:self.deleteButton 
       toString:@"Delete"]; 
[self.deleteButton setAction:@selector(deleteButtonClicked:)]; 
[self.deleteButton setTarget:self]; 

及其解決....