Rotation Transformation for UITextField, but text is displaced
I am trying to rotate UITex开发者_Go百科tField at 90 angle.It is rotationg perfectly..but text inside the UITextField, is displaced.I am unable to find out the reason behind it.
[InputField setTransform:CGAffineTransformMakeRotation(-M_PI / 2.0)];
[InputField setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
[InputField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
works for me!
What exactly is the problem? When I apply an CGAffineTransformation to an TextField, everything looks fine.
myTextField.transform = CGAffineTransformMakeRotation(M_PI_2);
Instead of setting the vertical and horizontal alignment (which produces the off-center edit window observed above), you could handle the editing rect manually. I overrided editingRectForBounds as follows. This centers the editing rect regardless of rotation:
-(CGRect)editingRectForBounds:(CGRect)bounds
{
// how tall is the font?
CGFloat fontHeight = [self.text sizeWithFont:self.font].height;
// compute pad
CGFloat pad = (bounds.size.height - fontHeight) / 2.0f;
// return rect with pad on top and bottom
return CGRectMake(bounds.origin.x, bounds.origin.y + pad, bounds.size.width, fontHeight);
}
精彩评论