UITextfield的位置设置不正确

时间:2014-11-28 05:41:08

标签: ios objective-c uitextfield

我正在创建一个应用程序,我想设置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;

i want the text starting from the topleft corner of uitextfield

3 个答案:

答案 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位置。