Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Overriding <code>drawPlaceholderInRect:</code> would be the correct way, but it does not work due to a bug in the API (or the documentation).</p> <p>The method never gets called on an <code>UITextField</code>.</p> <p>See also <a href="https://stackoverflow.com/questions/1920783/drawtextinrect-on-uitextfield-not-called/2581866#2581866">drawTextInRect on UITextField not called</a></p> <p>You might use digdog's solution. As I am not sure if that gets past Apples review, I chose a different solution: Overlay the text field with my own label which imitates the placeholder behaviour.</p> <p>This is a bit messy though. The code looks like this (Note I am doing this inside a subclass of TextField):</p> <pre><code>@implementation PlaceholderChangingTextField - (void) changePlaceholderColor:(UIColor*)color { // Need to place the overlay placeholder exactly above the original placeholder UILabel *overlayPlaceholderLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + 8, self.frame.origin.y + 4, self.frame.size.width - 16, self.frame.size.height - 8)] autorelease]; overlayPlaceholderLabel.backgroundColor = [UIColor whiteColor]; overlayPlaceholderLabel.opaque = YES; overlayPlaceholderLabel.text = self.placeholder; overlayPlaceholderLabel.textColor = color; overlayPlaceholderLabel.font = self.font; // Need to add it to the superview, as otherwise we cannot overlay the buildin text label. [self.superview addSubview:overlayPlaceholderLabel]; self.placeholder = nil; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload