1

Hi i am beginner in ios and i am trying to insert UItextfield "text" data exactly at center of the y-axis as like below textfield image

And i would like to insert UIbutton "titleLabel text" exactly center of x-axis and center of y-axis as like below button image for this i tried lot but i did not get result please help me some one

my code:-

 [self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);

    self.TripIdTxt.backgroundColor = [UIColor whiteColor];
    self.TripIdTxt.layer.cornerRadius = 3.0f;
    self.TripIdTxt.layer.masksToBounds = YES;
    [self.TripIdTxt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self.TripIdTxt setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    self.TripIdTxt.textColor = [UIColor blackColor];
    self.TripIdTxt.font = [UIFont fontWithName:bitter_Regular size:14];

enter image description here

  • Did u tried label.textAlignment = NSTextAlignmentCenter ? – LoVo Oct 19 '15 at 06:33
  • i can only set title label center of x axis but i am not able set that center of y-axis also using your answer –  Oct 19 '15 at 06:38
  • OK, maybe you should try to work with NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.minimumLineHeight = "YOUR FONTSIZE" + 5; style.maximumLineHeight = YOUR FONTSIZE" + 5; NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style}; yourLabel.setAttributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", attributes:attributtes]]; – LoVo Oct 19 '15 at 06:43

3 Answers3

2

set

for UIButton

yourbtnName.titleLabel.textAlignment = NSTextAlignmentCenter;

for y-axis

[yourbtnName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

for X-axis

[yourbtnName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

for UITextFields

yourtextfieldName.textAlignment = NSTextAlignmentCenter;

diectly you can access in storyboard like

enter image description here

for additional information see this link

Padding with TextField

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
TripIdTxt.leftView = paddingView;
self. TripIdTxt.leftViewMode = UITextFieldViewModeAlways;

or else use Transform

TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • yes andbu.kartik but using your answer i can only set title label center of x axis but i am not able set that center of y-axis also –  Oct 19 '15 at 06:37
  • @Anil -- check the updated answer, it apply for textfield also – Anbu.Karthik Oct 19 '15 at 06:40
  • @Anil -- have a happy day my bro – Anbu.Karthik Oct 19 '15 at 06:48
  • Anbu.Kartik one small help when i apply leftpadding view for UItextfild agin problem is coming i mean textfield text is not set at the center of x-axis [self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"]; self.TripIdTxt.leftViewMode = UITextFieldViewModeAlways; UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)]; self.TripIdTxt.leftView = paddingView; –  Oct 19 '15 at 07:01
  • why can you tell me reson please? –  Oct 19 '15 at 07:02
  • if you need to change the entire textfiled text as center the above answer is working fine , if you want to chnage the some left use Padding check the updated answer – Anbu.Karthik Oct 19 '15 at 07:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92692/discussion-between-anil-and-anbu-karthik). –  Oct 19 '15 at 07:12
0

Set the text alignmaent center of your lable property .

mainlabel.textAlignment = NSTextAlignmentCenter;
Garry
  • 407
  • 2
  • 15
0

u set its alignment using xib by

enter image description here

u can also set its vertical alignment using xib by

enter image description here

Programmatically U can set Content's vertical alignment for textfield as well as button as

button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
txtField.contentVerticalAlignment = UIControlContentHorizontalAlignmentRight;

Hope it helps. Happy Coding.. :)

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19