48 lines
1.9 KiB
Objective-C
48 lines
1.9 KiB
Objective-C
//
|
|
// HSChooserVerticallyCenteringTextFieldCell.m
|
|
// chooser
|
|
//
|
|
// Created by Chris Jones on 03/05/2019.
|
|
// Copyright © 2019 Hammerspoon. All rights reserved.
|
|
//
|
|
|
|
#import "HSChooserVerticallyCenteringTextFieldCell.h"
|
|
|
|
@implementation HSChooserVerticallyCenteringTextFieldCell
|
|
|
|
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
|
|
NSAttributedString *attrString = self.attributedStringValue;
|
|
|
|
/* if your values can be attributed strings, make them white when selected */
|
|
if (self.isHighlighted && self.backgroundStyle==NSBackgroundStyleDark) {
|
|
NSMutableAttributedString *whiteString = attrString.mutableCopy;
|
|
[whiteString addAttribute: NSForegroundColorAttributeName
|
|
value: [NSColor whiteColor]
|
|
range: NSMakeRange(0, whiteString.length) ];
|
|
attrString = whiteString;
|
|
}
|
|
|
|
[attrString drawWithRect: [self titleRectForBounds:cellFrame]
|
|
options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin];
|
|
}
|
|
|
|
- (NSRect)titleRectForBounds:(NSRect)theRect {
|
|
/* get the standard text content rectangle */
|
|
NSRect titleFrame = [super titleRectForBounds:theRect];
|
|
|
|
/* find out how big the rendered text will be */
|
|
NSAttributedString *attrString = self.attributedStringValue;
|
|
NSRect textRect = [attrString boundingRectWithSize: titleFrame.size
|
|
options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin ];
|
|
|
|
/* If the height of the rendered text is less then the available height,
|
|
* we modify the titleRect to center the text vertically */
|
|
if (textRect.size.height < titleFrame.size.height) {
|
|
titleFrame.origin.y = theRect.origin.y + (theRect.size.height - textRect.size.height) / 2.0;
|
|
titleFrame.size.height = textRect.size.height;
|
|
}
|
|
return titleFrame;
|
|
}
|
|
|
|
@end
|