我正在创建一个应用程序,我想设置UITextfield
的位置。但我被困在其中,它只设置vertical position
而不是horizontal position
。下面是我的代码和屏幕截图
Msgtextfield.placeholder=@"demo";
CGRect frame = CGRectMake(100,100,10,100);
UILabel * leftView = [[UILabel alloc] initWithFrame:frame];
leftView.backgroundColor = [UIColor clearColor];
self.Msgtextfield.leftView = leftView;
self.Msgtextfield.leftViewMode = UITextFieldViewModeAlways;
self.Msgtextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.Msgtextfield.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
答案 0 :(得分:2)
使用CATransform3DMakeTranslation
不要忘记导入Quartzcore framework
self.Msgtextfield.layer.sublayerTransform=CATransform3DMakeTranslation(5, 0, 0);
答案 1 :(得分:1)
UITextField.layer.sublayerTransform=CATransform3DMakeTranslation();
此行有效..
答案 2 :(得分:0)
制作Textfield的类别类。
<强>的UITextField + bgImageset.h 强>
@interface UITextField (bgImageset)
-(CGRect)textRectForBounds:(CGRect)bounds;
-(CGRect)editingRectForBounds:(CGRect)bounds;
<强>的UITextField + bgImageset.m 强>
@implementation UITextField (bgImageset)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x+10 , bounds.origin.y ,
bounds.size.width - 10 , bounds.size.height );
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
导入此类以更改光标位置并更改文本字段光标的X位置。