3

I'm kind of newbie in Objective-C and I need some help My problem is that I have a subclass of UITextField, and I want to set the placeholder font to be custom with this code

- (void)drawPlaceholderInRect:(CGRect)rect {
[[self placeholder] drawInRect:rect withAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Georgia Belle" size:25],NSForegroundColorAttributeName:RGBA(174, 150, 108, 1)}]; } 

and I have the next issue. When I select iPad in the simulator, and I compile the program, everything goes as it should, but when I select a retina device I get the next Exception:

 [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0].

What I'm asking is: why it works on non-retina but throws exceptions on retina, any kind of retina device.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Bogdan Somlea
  • 604
  • 3
  • 15
  • I would split your method into single chunks. First get the font in a variable, the the color, and then call the method. Now you can step-by-step debug the code and see why you probably get no font object. – Flovdis Jul 02 '14 at 14:10
  • i've tried, but it still happens – Bogdan Somlea Jul 02 '14 at 14:13
  • 1
    What did your debugger told you. Did you actually got a NSFont object? – Flovdis Jul 02 '14 at 14:16
  • As suggested by @Flovdis, I'd log each values of the attributes dictionary. – Larme Jul 02 '14 at 14:21
  • yes, i've got, but that's obvious, because otherwise it would not work on non-retina device, as i said: On Non-retina device it's working. – Bogdan Somlea Jul 02 '14 at 14:56
  • Why are you subclassing `UITextField` for this? Just use the `attributedPlaceholder` property of `UITextField`. – rmaddy Jul 02 '14 at 15:52
  • You should not assume the fonts available are the same on both classes of device and ignore the suggestions to approach this step-by-step. Breaking problems down is how we solve them. – Steven Fisher Jul 02 '14 at 15:57
  • when i split it, and i NSLog the font, this is what i've got. font-family: ".HelveticaNeueInterface-Regular"; font-weight: normal; font-style: normal; font-size: 17.00pt. it make-s no sense. – Bogdan Somlea Jul 03 '14 at 06:50
  • I hope my answer can help you – Gabriel.Massana Jul 03 '14 at 09:32

2 Answers2

2

Your problem is with your Font.

HelveticaNeueInterface-Regular is the System font. Like: [UIFont systemFontOfSize:25]

In the other hand I can't use RGBA(174, 150, 108, 1) I've got an issue in my code.

You should be sure that you have the font named "Georgia Belle" is in your project well imported.

Drag and drop Georgia Belle.ttf to your project

enter image description here

Open Info.plist and add a new line:

enter image description here

Open your Target / Build Phases / Copy Bundle Resources
Xcode fails here and sometimes its not adding our new font:

enter image description here

Click +

enter image description here

Done

enter image description here

Problem solved.

enter image description here enter image description here

You can download my solution from GitHub

One last thing. This is a nice website with all the standard ios fonts: http://iosfonts.com

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • you can't use RGBA(r,g,b,alpha) because it's a macro i'm using for color with RGBA. – Bogdan Somlea Jul 03 '14 at 10:12
  • 1
    it worked, the only think i haven't done,was, adding the font in my "copy bundle resources", but i can't figure out, why it worked on non-retina device – Bogdan Somlea Jul 03 '14 at 10:13
  • Nice! I neither can't understand the difference between Retina and non-Retina. In my code wasn't working in both. – Gabriel.Massana Jul 03 '14 at 10:20
  • i have another question based on this: One of my co-workers told me that, it's not ok to call the drawInRect method, is there any other way to change the placeholder style, without calling this method, and why it's not ok? – Bogdan Somlea Jul 03 '14 at 11:36
1

You have to add the font to the copy bundle resources

Root
  • 21
  • 2