UITextFieldってデフォルトだと左に余白も設定されてて良いのですが。
Border Styleを無しに設定すると余白(padding)も無くなっちゃいます。
テキストフィールド(UITextField)の左にpaddingを入れる
上のサイトに記載されている通りに書けばOKです。
UIView *paddingView = [ [UIView alloc] initWithFrame:CGRectMake(0, 0, 7, textField.frame.size.height)]; textField.leftView = paddingView; textField.leftViewMode = UITextFieldViewModeAlways;
で、複数のUITextFieldを一括で余白指定するにはどうすれば良いんだろ?
と思ったんですが。
UITextFieldのカスタムクラスを作って、StoryBoardで指定するのが一番スッキリしそう。
まずはUITextFieldのカスタムクラスを作成。

UIPaddingTextField.h
#import <uikit /UIKit.h> @interface UIPaddingTextField : UITextField @end
UIPaddingTextField.m
#import "UIPaddingTextField.h"
@implementation UIPaddingTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
// 余白設定
UIView *paddingView = [ [UIView alloc] initWithFrame:CGRectMake(0, 0, 7, self.frame.size.height)];
self.leftView = paddingView;
self.leftViewMode = UITextFieldViewModeAlways;
}
@end
で、StoryBoardの該当のテキストフィールドを選択して、先ほど作成したクラスを指定。
これで実行すると。

Border Style無しのテキストフィールドでも余白が指定されています。
株式会社woodsmallの小林でした。
https://woodsmall.co.jp/

コメント