How to Vertically Center Uitextfield Text

How do I vertically center UITextField Text?

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

In swift use:

textField.contentVerticalAlignment = UIControlContentVerticalAlignment.center

iOS UiTextField: Text wont center vertically

Seems to be a bug with the storyboard. It looks ok on emulator and device. Looks like it happens when you have rounded corners and font size < 21 or using a fixed height of the text field.

Workaround: Use intrinsic height with any border style except rounded. If using rounded corners then font size must be 21 or higher (regular system font). But this bug does not affect the running app, so you don't have to bother.

UITextField vertical text alignment on transform

try may work for u

  textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

Vertically centering a UITextField's attributedPlaceholder

Use NSParagraphStyle to increase minimum line height:

NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - [UIFont fontWithName:@"Gotham-BookItalic" size:14.0].lineHeight) / 2.0;

self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder text"
attributes:@{
NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],
NSFontAttributeName : [UIFont fontWithName:@"Gotham-BookItalic" size:14.0],
NSParagraphStyleAttributeName : style
}
];

You could also override a - (CGRect)placeholderRectForBounds:(CGRect)bounds; method in UITextField subclass.

It's messy, but it works :)

How to center UITextField text in Swift

You would want to write it like this:

myUITextObject.textAlignment = .center

(Edited to change from .Center to .center)

UITextField text not appearing at the center

For vertical alignment:

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

Centered both vertically and horizontally:

theTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
theTextField.textAlignment = NSTextAlignmentCenter;

Centered within the parent container:

YourTextFieldName.center = parentViewField.center;

Center text vertically in a UITextView

First add an observer for the contentSize key value of the UITextView when the view is loaded:

- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}

Then add this method to adjust the contentOffset every time the contentSize value changes:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}


Related Topics



Leave a reply



Submit