UITextField的文本插入?

我想插入一個UITextField文本框架

這可能嗎? atom


#1樓

使用textRectForBounds:是正確的方法。 我將其包裝在子類中,所以您能夠簡單地使用textEdgeInsets 。 參見SSTextFieldspa


#2樓

我可以經過如下方式作到這一點: code

myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);

固然,請記住導入QuartzCore並將框架添加到您的項目中。 orm


#3樓

若是隻須要左邊距,則能夠嘗試如下操做: get

UItextField *textField = [[UITextField alloc] initWithFrame:...];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.frame.size.height)];
leftView.backgroundColor = textField.backgroundColor;
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;

這個對我有用。 但願對您有所幫助。 it


#4樓

若是隻想更改TOP和LEFT縮進,則 io

//佔位符位置 form

- (CGRect)textRectForBounds:(CGRect)bounds {

CGRect frame = bounds;
frame.origin.y = 3;
 frame.origin.x = 5;
bounds = frame;
return CGRectInset( bounds , 0 , 0 );
}

//文字位置 import

- (CGRect)editingRectForBounds:(CGRect)bounds {

CGRect frame = bounds;
frame.origin.y = 3;
 frame.origin.x = 5;
bounds = frame;
return CGRectInset( bounds , 0 , 0 );
}

#5樓

向UITextField添加填充的一種好方法是子類化UITextField並添加edgeInsets屬性。 而後,您設置edgeInsets,並將相應繪製UITextField。 使用自定義的leftView或rightView集也能夠正常運行。

OSTextField.h

#import <UIKit/UIKit.h>

@interface OSTextField : UITextField

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSTextField.m

#import "OSTextField.h"

@implementation OSTextField

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

@end
相關文章
相關標籤/搜索