iOS 自定义textField 显示下划线 左边显示文字 自定义placeholder的颜色和字体

本文详细介绍了如何在文本框中实现下划线效果及左侧文字显示的功能,包括代码实现、关键方法重写及属性设置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇文章是记录下自己学习的内容 , 代码比较low,请各位大神多多指教


效果为


主要实现方法:

实现下划线。重写drawRect方法:

- (void)drawRect:(CGRect)rect

{

    CGContextRef context = UIGraphicsGetCurrentContext();


    [[UIColor redColor] set];//设置下划线颜色 这里是红色 可以自定义

    

    CGFloat y = CGRectGetHeight(self.frame);

    CGContextMoveToPoint(context, 0, y);

    CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), y);

    //设置线的宽度

    CGContextSetLineWidth(context, 2);

    //渲染 显示到self

    CGContextStrokePath(context);

}


接下来自定义textField的leftView, 这里因为要显示文字,所以用一个label来实现, 也可以使用自定义的view

。在初始化的时候设置label为leftView, 并设置leftView的mode

- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        UILabel *label = [[UILabel alloc] init];

        self.leftView = label;

        self.leftViewMode = UITextFieldViewModeAlways;

        _leftLabel = label;

    }

    return self;

}

_leftLabel是自己定义的一个全局变量, 这里可以看到leftLabel并没有frame

接下来重写textField的

- (CGRect)leftViewRectForBounds:(CGRect)bounds方法, 给leftView设置frame

- (CGRect)leftViewRectForBounds:(CGRect)bounds

{

    

    [super leftViewRectForBounds:bounds];

    CGRect frame = bounds;

    //这里设置为leftView的宽是textField的0.3倍,

    frame.size.width = bounds.size.width * 0.3;

    

    return frame;

}

然后在.h文件里面生成属性, 方便外界修改;

//左侧显示的文字

@property (nonatomic , copy) NSString *leftTitle;

//左侧文字的颜色

@property (nonatomic , strong) UIColor *leftTitleColor;

/**只有在设置了placeholder之后才会有效**/

@property (nonatomic , strong) UIColor *placeholderColor;


@property (nonatomic , strong) UIFont *placeholderFont;Font;

接下来重写属性的set方法


- (void)setPlaceholderColor:(UIColor *)placeholderColor

{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    dict[NSForegroundColorAttributeName] = placeholderColor;

    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];

    [self setAttributedPlaceholder:attribute];

}

- (void)setPlaceholderFont:(UIFont *)placeholderFont

{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    dict[NSFontAttributeName] = placeholderFont;

    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];

    [self setAttributedPlaceholder:attribute];

}

- (void)setLeftTitleColor:(UIColor *)leftTitleColor

{

    _leftTitleColor = leftTitleColor;

    [_leftLabel setTextColor:leftTitleColor];

}

- (void)setLeftTitle:(NSString *)leftTitle

{

    _leftTitle = leftTitle;

    [_leftLabel setText:leftTitle];

}

说明:之所以要在设置placeholder之后再设置placeholderColor和placeholderFont是因为我是用的是    [self setAttributedPlaceholder:attribute];来设置的, 这里是通过初始化self。placeholder来实现, 如果先设置placeholderColor,因为此时placeholder为空, 会设置失败, placeholderFont同上,


到这里就全部设置完毕了。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值