美文网首页键盘上的鼓手iOS 代码相关iOS开发
ios修改textField的placeholder,光标,所

ios修改textField的placeholder,光标,所

作者: 武小寺 | 来源:发表于2015-11-04 16:03 被阅读3884次

    占位符的颜色和大小

    textField.placeholder = @"username is in here!";  
    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];  
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"]; 
    

    光标颜色

        self.tintColor= [UIColor whiteColor];
    

    重新处理子视图的位置

    //控制placeHolder的位置
    -(CGRect)placeholderRectForBounds:(CGRect)bounds
    {
        CGRect inset = CGRectMake(bounds.origin.x+20, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
        return inset;
    }
    
    //控制显示文本的位置
    -(CGRect)textRectForBounds:(CGRect)bounds
    {
        CGRect inset = CGRectMake(bounds.origin.x+20, bounds.origin.y, bounds.size.width -10, bounds.size.height);
        return inset;
    }
    
    //控制编辑文本的位置
    -(CGRect)editingRectForBounds:(CGRect)bounds
    {
        CGRect inset = CGRectMake(bounds.origin.x +20, bounds.origin.y, bounds.size.width -10, bounds.size.height);
        return inset;
    }
    

    监听事件

     [self.positionTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    // 去事件中处理
    - (void)textFieldDidChange:(UITextField *)textField{
        if ([textField.text isWordMoreThan:11]) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
    

    这里是我写的类,可以根据自己的需求增删功能
    .h

    
    #import <UIKit/UIKit.h>
    
    @interface BBTextField : UITextField
    
    @end
    

    .m

    #import "BBTextField.h"
    
    @interface BBTextField ()
    
    @end
    
    
    @implementation BBTextField
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            
            [self setUpUI];
    
            
        }
        return self;
    }
    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
        [self setUpUI];
    }
    
    - (void)setUpUI
    {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 22;
        self.backgroundColor = Default_BackColor;
        self.layer.borderColor = Default_SeparatorColor.CGColor;
        self.layer.borderWidth = 1;
        self.font = [UIFont systemFontOfSize:12];
        //字体颜色
        self.textColor = [UIColor whiteColor];
        //光标颜色
        self.tintColor= [UIColor whiteColor];
        //占位符的颜色和大小
        [self setValue:Default_FontColor forKeyPath:@"_placeholderLabel.textColor"];
        [self setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
        
    }
    
    
    //控制placeHolder的位置
    -(CGRect)placeholderRectForBounds:(CGRect)bounds
    {
        CGRect inset = CGRectMake(bounds.origin.x+20, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
        return inset;
    }
    
    //控制显示文本的位置
    -(CGRect)textRectForBounds:(CGRect)bounds
    {
        CGRect inset = CGRectMake(bounds.origin.x+20, bounds.origin.y, bounds.size.width -10, bounds.size.height);
        return inset;
    }
    
    //控制编辑文本的位置
    -(CGRect)editingRectForBounds:(CGRect)bounds
    {
        CGRect inset = CGRectMake(bounds.origin.x +20, bounds.origin.y, bounds.size.width -10, bounds.size.height);
        return inset;
    }
    

    相关文章

      网友评论

        本文标题: ios修改textField的placeholder,光标,所

        本文链接:https://www.haomeiwen.com/subject/xooqhttx.html