XCode:如何在文本字段上添加左右填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18682390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:46:01  来源:igfitidea点击:

XCode : How to Add Left and Right Padding on Text Field?

iosobjective-cxcode

提问by Saint Robson

I have this .h code :

我有这个 .h 代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
@property (strong, nonatomic) IBOutlet UILabel *titleLogin;

- (IBAction)buttonRegister;
- (IBAction)buttonLogin;
- (IBAction)loginFacebook;
- (IBAction)loginTwitter;

@end

and this is my .m code :

这是我的 .m 代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [_fieldEmail setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
    [_titleLogin setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonRegister {
}

- (IBAction)buttonLogin {
}

- (IBAction)loginFacebook {
}

- (IBAction)loginTwitter {
}
@end

now, what I want to do is to add padding on left and right of these 2 text fields : fieldEmailand fieldPasswordso it has some space because I put an image as background on both text fields.

现在,我想要做的是在左,右这两个文本字段添加填充:fieldEmailfieldPassword因此具有一定的空间,因为我把图像作为这两个文本字段的背景。

I tried to add this code on my .m file under - (void)viewDidLoad:

我尝试将此代码添加到我的 .m 文件中- (void)viewDidLoad

UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[UITextField setLeftViewMode:UITextFieldViewModeAlways];
[UITextField setLeftView:fieldEmail];

but my XCode gave me this error message :

但我的 XCode 给了我这个错误信息:

no known class method for selector 'setLeftViewMode'
no known class method for selector 'setLeftView'

by the way, I'm using XCode 4.6.3. does this version have special code to add padding on text field? thank you.

顺便说一下,我使用的是 XCode 4.6.3。此版本是否有特殊代码可以在文本字段上添加填充?谢谢你。

回答by Mundi

leftViewModeand leftVieware properties of UITextField, not class methods. You need to assign them to instances.

leftViewModeleftView是 的属性UITextField而不是类方法。您需要将它们分配给实例。

yourTextField.leftViewMode = UITextFieldViewModeAlways;
yourTextField.leftView     = fieldEmail;