zoukankan      html  css  js  c++  java
  • 自己定义UITextField

    目的是实现例如以下的效果:


    UITextField的leftView是自己定义的UIView,当中:

    1、包括一个居中显示的icon。而且上,左,下各有1px的间隙

    2、左上和左下是圆角,右边没有圆角

    居中展示icon

    关键是leftView不是UIImageView。而是把UIImageView设置为subview

    CGFloat height = frame.size.height;
            
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, height - 1, height - 2)];
            
    UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
    icon.frame = CGRectMake(height / 4, height / 4, height / 2, height / 2);
    [leftView addSubview:icon];
    上面的代码,使icon在leftView居中显示

    制造1px间隙

    top和bottom的间隙非常easy做,仅仅要令leftView的height比UITextField的height少2。然后y设置为1

    左側的间隙比較麻烦,首先将width设置为比UITextField的width少1。可是直接设置x为1无效。须要override下面方法:

    -(CGRect) leftViewRectForBounds:(CGRect)bounds {
        CGRect iconRect = [super leftViewRectForBounds:bounds];
        iconRect.origin.x += 1;
        return iconRect;
    }

    制造部分圆角

    制造全部的圆角比較简单,仅仅须要:

    self.layer.cornerRadius = 5;

    可是要制造部分圆角,就比較麻烦:

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:leftView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = leftView.bounds;
    maskLayer.path = maskPath.CGPath;
    leftView.layer.mask = maskLayer;

    创建阴影

    self.layer.shadowColor = [UIColor colorWithRed:132/255.0f green:214/255.0f blue:219/255.0f alpha:1.0f].CGColor;
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowOffset = CGSizeMake(3, 3);

    完整的代码

    @implementation LosTextField
    
    -(id)initWithFrame:(CGRect)frame Icon:(NSString*)iconName
    {
        self = [super initWithFrame:frame];
        if (self) {
            
            CGFloat height = frame.size.height;
            
            UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, height - 1, height - 2)];
            leftView.backgroundColor = [UIColor colorWithRed:132/255.0f green:214/255.0f blue:219/255.0f alpha:1.0f];
            
            UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
            icon.frame = CGRectMake(height / 4, height / 4, height / 2, height / 2);
            [leftView addSubview:icon];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:leftView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = leftView.bounds;
            maskLayer.path = maskPath.CGPath;
            leftView.layer.mask = maskLayer;
            
            self.leftView = leftView;
            self.leftViewMode = UITextFieldViewModeAlways;
        }
        return self;
    }
    
    -(CGRect) leftViewRectForBounds:(CGRect)bounds {
        CGRect iconRect = [super leftViewRectForBounds:bounds];
        iconRect.origin.x += 1;
        return iconRect;
    }
    
    @end

    self.username = [[LosTextField alloc] initWithFrame:CGRectMake(20, 150, 280, 40) Icon:@"login_username"];



  • 相关阅读:
    javascript里面this机制的几个例子
    把List<string>集合,编程string,并以“,”号分割
    比较集合List<T>集合,前后多了哪些数据,少了哪些数据Except
    c# Web.config中 windows连接数据库
    MVC之图片验证码
    匿名函数-简单实例
    c# 如何找到项目中图片的相对路径
    MVC下 把数据库中的byte[]值保存成图片,并显示在view页面
    MVC下form表单一次上传多种类型的图片(每种类型的图片可以上传多张)
    关于Visual Studio未能加载各种Package包的解决
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6840599.html
Copyright © 2011-2022 走看看