Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom "placeholder" on UITextField being truncated unexpectedly
    primarykey
    data
    text
    <p>I'm trying to create a text field with a persistent "placeholder" of sorts by adding a UILabel as a subview of the text field. However, my label keeps being truncated, and I can't figure out why. Any advice is much appreciated!</p> <p>Here's what I'm currently getting (it's supposed to say "first name": <img src="https://imgur.com/58PEPW4.png" alt="Current"></p> <p>Here's the code I have: </p> <pre><code>#import "LabeledTextField.h" @interface PlaceholderLabel : UILabel @end @implementation PlaceholderLabel -(void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 10, 0, 0}; return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } @end @interface LabeledTextField () @property (nonatomic) PlaceholderLabel *placeholderLabel; @end @implementation LabeledTextField - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _placeholderLabel = [[PlaceholderLabel alloc] initWithFrame:CGRectMake(10, [self frame].size.height/2, 0, 0)]; [_placeholderLabel setBackgroundColor:UIColorFromRGB(0xdedede)]; [_placeholderLabel setTextColor:UIColorFromRGB(0x787878)]; [_placeholderLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:kTextFieldPlaceholderFontSize]]; [_placeholderLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [_placeholderLabel setNumberOfLines:1]; } return self; } -(void)layoutSubviews { [super layoutSubviews]; [self setLeftView:_placeholderLabel]; } -(void)drawPlaceholderInRect:(CGRect)rect { return; } -(void)setPlaceholder:(NSString *)placeholder { [_placeholderLabel setText:placeholder]; [_placeholderLabel sizeToFit]; } -(CGRect)leftViewRectForBounds:(CGRect)bounds { CGRect frame = [_placeholderLabel frame]; frame.origin.y = (self.frame.size.height - frame.size.height)/2; frame.size = [[_placeholderLabel text] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:kTextFieldPlaceholderFontSize]]; return frame; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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