UITextField and UILabel vertical alignment

Discussion in 'iOS Development' started by acca2o, Dec 26, 2009.

  1. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    I insert a UITextField into a UITableViewCell to get user input.
    I used the UITableViewCell textLabel.bounds an textLabel.font properties to set UITextField instance.
    It's ok, but the UITextField vertical alignment is TOP and non CENTER like in the textLabel
    Looking at the UITextField class reference I discovered it inherits fron UIControl that has a contentVerticalAlignment property.
    But it seems UITextField doesn't inherit this property.
    Is there an easy way to center verticaly the text in the UITextField?
    Thanks
    p
  2. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    you did

    [objc]contentVerticalAlignment = UIControlContentVerticalAlignmentCenter[/objc]

    posted your UITextField code.
  3. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    If I try to use that property I get the following error:
    request for member contentVerticalAlignment in something not a structure or union

    this is my code:
    [OBJC]
    ...
    newProfileTextField = [[UITextField alloc] initWithFrame:newProfileCell.textLabel.bounds];
    newProfileTextField.keyboardType = UIKeyboardTypeAlphabet;
    newProfileTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
    newProfileTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    newProfileTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    newProfileTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    newProfileTextField.delegate = self;
    newProfileTextField.font = newProfileCell.textLabel.font;
    [newProfileCell.contentView addSubview:newProfileTextField];[newProfileTextField release];
    [newProfileTextField becomeFirstResponder];
    ...
    [/OBJC]
  4. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    you could try
    [objc]newProfileTextField = [[UITextField alloc] initWithFrame:CGRectZero];[/objc]

    I noticed [newProfileTextField release] is before [newProfileTextField becomeFirstResponder]; that becomeFirstResponder method calls might be not work
  5. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You don't want to init the textfield with the 'bounds' of another view. The 'bounds' is a CGRect struct with 0,0 as the origin, and the same width and height as the view's frame. That's probably why the text is showing up in the wrong spot.

    @NolesFan - the -[addSubview:] method increments the retain count of the subview (argument), so the subsequent -[release] message is perfectly valid, returning the retain count to 1, and not causing the object to be destroyed.

Share This Page