7

I'm using UITextField as a UISearchBar replacement and "stealing" the magnifying glass icon from the original UISearchBar with this crazy code:

    UISearchBar *originalSearchBar = [[UISearchBar alloc] init];
    for (UIView *searchBarSubview in [originalSearchBar subviews]) {
        if([searchBarSubview isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)searchBarSubview;
            [_textField setLeftView:[textField leftView]];
            [_textField setLeftViewMode:UITextFieldViewModeAlways];
        }
    }

As you've probably guessed, I don't want to use my own bitmap.

Isn't there an easier accessible magnifying glass icon somewhere in Cocoa?

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118

4 Answers4

26

So, here's the code with the unicode character:

    UILabel *magnifyingGlass = [[UILabel alloc] init];
    [magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
    [magnifyingGlass sizeToFit];

    [textField setLeftView:magnifyingGlass];
    [textField setLeftViewMode:UITextFieldViewModeAlways];

Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
  • the resulting image look great! Thanks a lot! – Zennichimaro Apr 10 '13 at 09:19
  • 1
    Google "Emoji" for more informations about that . Your guys will fall in love with "Emoji" . For Example : http://apps.timwhitlock.info/emoji/tables/unicode – isaacselement Mar 06 '14 at 07:53
  • This is a great solution with one problem, magnifying glass is 3d glass and in modern iOS7 and iOS8, 3d designs have been removed and every thing is in a plane format. So designs do not look consistent. But we can use UIImage for plane design. – AsifHabib Nov 21 '14 at 10:56
  • How do you add the unicode variation selector? Can't get the plain magnifying glass. – fabb Mar 05 '15 at 07:22
  • When writing @"\U0001F50D\U000025B6" I always get the 3d glass and a right arrow as a second character. – fabb Mar 05 '15 at 07:53
  • I tried with the Unicode variation selector U0000FE0E, but it did not work either. http://stackoverflow.com/a/13836045/354018 – fabb Mar 05 '15 at 08:01
  • This does not work for me, do i need to set a specific font? I am using Xamarin – Mingo Apr 07 '15 at 07:41
  • The answer in general is good but the edit is not working for me. – Julian Osorio Apr 17 '15 at 01:39
  • How to add alignment for this magnifyingGlass? It should be before placeholder text. – Piotr Wasilewicz Nov 30 '16 at 15:40
3

I don't know of any standard image for it in Cocoa (and there is no character for it in Unicode either).

An image for this is part of the Dock bundle, however:

/System/Library/CoreServices/Dock.app/Contents/Resources/ectl_search_magnify.png
Kevin Grant
  • 5,363
  • 1
  • 21
  • 24
  • Actually, there is a unicode character! I've tried it and it looks good: http://www.fileformat.info/info/unicode/char/1f50d/index.htm – Rudolf Adamkovič Aug 05 '12 at 10:05
  • Nice...I seem to recall having trouble making that work at one point (perhaps it wasn't supported in the fonts of all OS versions?). In any case it's there now...it is even "in color". So, use that. :) – Kevin Grant Aug 05 '12 at 15:34
2

As I couldn't get a plain unicode magnifying glass to appear, I decided to see what a standard UISearchBox uses. It turns out it's an image of a magnifying glass, which I've extracted and included below, though it's a very simple shape which would be trivial to reproduce perfectly.

2x Search 3x Search

Robert
  • 5,735
  • 3
  • 40
  • 53
0

Using OSX 10.7.5 I couldn't find the ectl_search_magnify.png but a simple

find /System -ipath '*magni*'

found:

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MagnifyingGlassIcon.icns
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Dark_NSSmallMagnifyingGlass.tiff
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Night_NSSmallMagnifyingGlass.tiff

The latter two are probably what you want (assuming you need an image).

Ehren
  • 504
  • 5
  • 13