年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9519|回复: 0

UITextField设置内边距的三种方式

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2015-1-7 09:47:02 | 显示全部楼层 |阅读模式
    1、利用UITextField的属性leftView,创建一个宽为5,高为20的View设置为UITextField的leftView,这样视觉上就会有5像素的左边距
    [Objective-C] 纯文本查看 复制代码
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;


    2、MAC OS中以及iOS7.0以前可以使用如下方法设置
    [Objective-C] 纯文本查看 复制代码
    [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingTop"];
    [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingLeft"];
    [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingBottom"];
    [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingRight"];


    3、自定义一个TextField,实现如下两个方法
    [Objective-C] 纯文本查看 复制代码
    //控制placeHolder 的位置,左右缩 8px
    - (CGRect)textRectForBounds:(CGRect)bounds
    {
        return CGRectInset(bounds , 8 , 0 );
    }
    
    // 控制文本的位置,左右缩 8px
    - (CGRect)editingRectForBounds:(CGRect)bounds
    {
        return CGRectInset(bounds , 8 , 0 );
    }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-28 02:46 , Processed in 0.046663 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表