Reputation: 1659
I currently have a standard NSSearchField
(in a nib). The height is fixed by Apple at 22px. The application supports or uses Tibetan Unicode. Tibetan characters are written left to right, but certain adjacent characters stack vertically which makes for an unusually large line height. Text entered into the search field is clipped. My solution is to subclass NSSearchFieldCell
and if necessarily NSSearchField
.
First problem: It's hard to figure out what methods to override. I find hints in the Apple documentation but nothing systematic. Any pointers are appreciated.
What I have so far: I subclassed NSSearchFieldCell
. I replaced searchTextRectForBounds
, cancelButtonRectForBounds
, searchButtonRectForBounds
, and finally drawWithFrame inView
.
I get a decent graphical image at the desired size. The cancel button is drawn in the expected location, but I have no idea where the cancel button controls are on the screen.
What other methods to I need to override?
To illustrate the problem, the following text ཀླུའི་རྒྱལ་པོ་
transliterated into Latin/Roman letters klu'i rgyal po in the search bar used by Finder. Notice the gigu "U" is clipped along the bottom.
Upvotes: 8
Views: 1775
Reputation: 1659
What I ended up doing:
(The end user seems happy with this solution, at least for now.) I echo the NSSearchField text as it's entered to a textfield in an NSPopover. I created a transient popover from a separate nib and keep it around. Then in the IBAction bound to the search bar I added the following code:
- (IBAction)SearchPartial:(id)sender {
NSInteger recordNumber;
DefinitionEntity *def;
searchText = [self.SearchField stringValue];
if (!popupShows) {
popupShows = YES;
NSRectEdge prefEdge = NSMinXEdge;
[searchBarPopover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:prefEdge];
[self.SearchField becomeFirstResponder];
//[[self.SearchField cell] setPlaceholderString:searchText];
[self.SearchField setStringValue:searchText];
[[self.SearchField currentEditor] setSelectedRange: NSMakeRange(0, 0)];
[[self.SearchField currentEditor] moveToEndOfLine:nil];
}
popoverViewController.textField.stringValue = searchText;
recordNumber = [self upDateSearch:searchText];
def = [self.lookupModel getEntrybyNumber:recordNumber];
[self.parent lookupSelectionDidChange:def];
}
One problem with popover's is that they become firstresponder when shown. I referenced this question's answer: show-nspopover-without-Focus. It's important to re-assign firstresponder to the search field before editing/restoring the text.
Upvotes: 0
Reputation: 33359
I wouldn't go down that road at all. Instead you should subclass NSView and stick an NSTextField + NSPopUpButton + NSButton inside it.
With a bunch of hacking you'll get it working but then OS X 10.10 will ship and it will be broken.
Upvotes: 3
Reputation: 3996
You need to make a custom NSSearchFieldCell subclass, and override drawWithFrame:, then set your NSSearchField to use that cell. You can either draw your custom border in code, or use end-cap images and a background image for the center that you tile horizontally. (I Haven't tried this)
Try that out.
Alternatively, just create a NSTextFeild, then update the view.
Upvotes: 2