如何创建自定义文本字段和textview

时间:2014-10-31 10:41:04

标签: ios objective-c ios7 textview textfield

如何创建文本字段如下图所示

在下图中地址是自定义文本视图。

Create the custom Textfield with only one single line.

2 个答案:

答案 0 :(得分:1)

txt_Username = [[UITextField alloc] initWithFrame:CGRectMake(0,40,self.view.frame.size.width/2,32)];
txt_Username.borderStyle = UITextBorderStyleRoundedRect;
txt_Username.font = [UIFont systemFontOfSize:15];
txt_Username.placeholder = @"PlacceHolder";
txt_Username.autocorrectionType = UITextAutocorrectionTypeNo;
txt_Username.autocapitalizationType = UITextAutocapitalizationTypeNone;
txt_Username.keyboardType = UIKeyboardTypeDefault;
txt_Username.returnKeyType = UIReturnKeyNext;
txt_Username.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_Username.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[txt_Username setLeftViewMode:UITextFieldViewModeAlways];
txt_Username.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user"]];

txt_Username.borderStyle = UITextBorderStyleNone;
[txt_Username setBackgroundColor:[UIColor clearColor]];
UIView *demoLine = [[UIView alloc] initWithFrame:CGRectMake(txt_Username.frame.origin.x
                                                            , txt_Username.frame.origin.y+txt_Username.frame.size.height
                                                            , txt_Username.frame.size.width, 1)];
[demoLine setBackgroundColor:[UIColor blackColor]];    
[self.view addSubview:demoLine];

试试这会有帮助。

答案 1 :(得分:0)

将文本字段边框样式更改为透明边框样式(在“属性”检查器中最左侧)。在UIImageView中使用图像作为行。在此图像上方放置您的文本字段。或者不使用图像作为线条,而是绘制线条。线描代码在这里:

-(void)drawLine:(CGFloat)fromX fromY:(CGFloat)fromY toX:(CGFloat)toX toY:(CGFloat)toY width:(CGFloat)width color:(UIColor *)color
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(fromX, fromY)];
    [path addLineToPoint:CGPointMake(toX, toY)];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [color CGColor];
    shapeLayer.lineWidth = width;
    shapeLayer.fillColor = [[UIColor clearColor] CGColor];

    [self.view.layer addSublayer:shapeLayer];
}

如果您使用避免线条绘图并坚持图像,那就更好了。