美文网首页
UITextField leftView边距和textRect问

UITextField leftView边距和textRect问

作者: Aldon丶 | 来源:发表于2017-08-18 13:28 被阅读0次

    如图:

    image.png

    左边的icon太过于贴近左边
    text输入也太贴近左边
    而且UITextfield也没有提供相应的方法
    不然就要让UI给切一个带有边距的图,或者添加一层父视图,其实并不需要

    解决办法:
    创建一个继承与UITextfield的类,重写以下方法

    //leftView添加左边距
    - (CGRect)leftViewRectForBounds:(CGRect)bounds{
        CGRect textRect = [super leftViewRectForBounds:bounds];
        textRect.origin.x += kSuitLength(10);
        return textRect;
    }
    
    //text位置添加左边距
    - (CGRect)textRectForBounds:(CGRect)bounds {
        CGRect rect = [super textRectForBounds:bounds];
        int margin = kSuitLength(9);
        CGRect inset = CGRectMake(rect.origin.x + margin, rect.origin.y, rect.size.width - margin, rect.size.height);
        return inset;
    }
    
    //编辑位置添加左边距
    - (CGRect)editingRectForBounds:(CGRect)bounds {
        CGRect rect = [super editingRectForBounds:bounds];
        int margin = kSuitLength(9);
        CGRect inset = CGRectMake(rect.origin.x + margin, rect.origin.y, rect.size.width - margin, rect.size.height);
        return inset;
    }
    

    当然右边距也是一样~
    效果图,这样看上去就顺眼很多了!


    image.png

    如有帮助,请点一下喜欢❤️❤️❤️

    相关文章

      网友评论

          本文标题:UITextField leftView边距和textRect问

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