zoukankan      html  css  js  c++  java
  • UITextField设置leftView的Insets

    Insets就是css中的padding

    我们给UITextField设置了leftView,目的是在文本输入框左側显示一个图标。可是在ios7里,这个图标会紧紧地挨着TextField的左边框,非常不美观,所以就希望设置一个Insets。可是直接设置ImageView的bounds不行,须要用以下这种方法:

    @interface YLSTextField : UITextField
    
    -(id)initWithFrame:(CGRect)frame Icon:(UIImageView*)icon;
    
    @end

    @implementation YLSTextField
    
    -(id)initWithFrame:(CGRect)frame Icon:(UIImageView*)icon
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.leftView = icon;
            self.leftViewMode = UITextFieldViewModeAlways;
        }
        return self;
    }
    
    -(CGRect) leftViewRectForBounds:(CGRect)bounds {
        CGRect iconRect = [super leftViewRectForBounds:bounds];
        iconRect.origin.x += 10;// 右偏10
        return iconRect;
    }
    
    @end

    UIImage *usernameImage = [UIImage imageNamed:@"user"];
    UIImageView *usernameIcon = [[UIImageView alloc] initWithImage:usernameImage];
    usernameIcon.frame = CGRectMake(0, 0, 20, 20);
            
    self.username = [[YLSTextField alloc] initWithFrame:CGRectMake(0, 0, 240, 30) Icon:usernameIcon];
    self.username.placeholder = @"用户名";
    self.username.borderStyle = UITextBorderStyleRoundedRect;
    self.username.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.username setKeyboardType:UIKeyboardTypeNumberPad];

    关键就是定义UITextField的子类,并覆盖其leftViewRectForBounds方法

  • 相关阅读:
    WebAPI下的如何实现参数绑定
    MYSQL主从不同步延迟原理
    mysql的limit经典用法及优化
    ASP.NET MVC中的模型绑定
    使用EF实现数据库的增删改查
    NoSQL数据库技术特性解析之文档数据库
    MySQL 缓存 Query Cache
    Loadrunner test web service which need username and password
    vb写文件时报'Invalid procedure call or argument'
    Shell 笔记
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4479851.html
Copyright © 2011-2022 走看看