0

I tried to set the right side margin in UItextfield but I can't get success for this.

I tried below code:

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ];
[lbl setText:@"Partenza:"];
[lbl setFont:[UIFont fontWithName:@"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
txtFCarat.delegate = self;

Please help me if any body have a solution for this!

3 Answers3

3

You can override this below two methods

@implementation rightTextField  

       // placeholder position

    - (CGRect)textRectForBounds:(CGRect)bounds {
         return CGRectInset( self.bounds , margin position , margin position );
    }

    // text position
    - (CGRect)editingRectForBounds:(CGRect)bounds {
         return CGRectInset( self.bounds , margin position ,margin position );
    }

Edit: use to bind code like

textField.bounds = [self editingRectForBounds:textField.bounds];
textField.bounds = [self textRectForBounds:textField.bounds];

Your expected Answer:

 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
    textfield.rightView = imageView;
    textfield.rightViewMode = UITextFieldViewModeAlways;
codercat
  • 22,873
  • 9
  • 61
  • 85
1

Add the below line where you are creating textfield..and set value as per requirement

textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);

Another way to achieve this by overriding the textRectForBounds

Let me to give complete Description how to do this or achieve it

  1. First you create a class and name "textField"(or what ever you Want) and must be sure that IT IS SUBCLASS OF UITextField.

    2.Now open the textfield.m class and use this code or paste below code in this class (textfield):

     - (CGRect)textRectForBounds:(CGRect)bounds {
       return CGRectMake(bounds.origin.x + 50, bounds.origin.y,
                  bounds.size.width - 20, bounds.size.height);
     }
    
    -(CGRect)editingRectForBounds:(CGRect)bounds {
     return [self textRectForBounds:bounds];
    }
    

If you want to customize the textfield more then you can use here only or in calling Class like

use the below code :Note it is only for testing purpose.it depend on your requirement

- (void)settingTextField {

[self setTextField];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}



- (void)setTextField {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}

3.improt textField.h class into your view-controller where you wants to create textfield

and you call it like this.

 #import "textField.h"

//Viewcontroller Class
- (void)viewDidLoad
  {
   [super viewDidLoad];
   textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
  field1.backgroundColor=[UIColor greenColor];
  field1.placeholder=@"0";
  [self.view addSubview:field1];
}

Try to adjust the value textRectForBounds as per you reqitement

  • In application i set Uitextfield alignment is right side. I try your code but it is not work for me. please help me. @kamalesh kumar yadav – Bhargav Narola Jan 01 '14 at 05:24
  • at place of 20 make it -10(minus sign 10) and let me now – kamalesh kumar yadav Jan 01 '14 at 05:25
  • i set 20 instate of 10 but in that Uitextfield width in changes. I need to set right margin when text is display and edit . @kamalesh kumar yadav – Bhargav Narola Jan 01 '14 at 05:45
  • now check the updated answer i was telling to make it -10 not just 10.just copy and paste the above code.Ihad made changes – kamalesh kumar yadav Jan 01 '14 at 05:47
  • little bit get success but one problem is there. we set -10 so Uitextfield width -10 decrease from origin width. but problem is there the text is being display in Uitextfield is also movie -10 from right side. – Bhargav Narola Jan 01 '14 at 05:56
-1
    NSString *myString = @"Partenza:";
    UIFont *myFont = [UIFont fontWithName:@"Verdana" size:12];
    CGSize myStringSize = [myString sizeWithFont:myFont];

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myStringSize.width + 10, 25) ];
    [lbl setText:myString];
    [lbl setFont:[UIFont fontWithName:@"Verdana" size:12]];
    [lbl setTextColor:[UIColor grayColor]];
    [lbl setTextAlignment:UITextAlignmentLeft];
    txtFCarat.rightView = lbl;
    txtFCarat.rightViewMode = UITextFieldViewModeAlways;
Abhishek
  • 1,682
  • 2
  • 17
  • 29