iOS中改变uiTextField的光标起始位置

uiTextField作为输入框,在很多场景都会使用,一般我们都是对齐默认的布局做一些更改,常用的可能会修改光标的颜色,placeholder的颜色,字体等等,有些修改直接设置uiTextField的属性就可以,有些则没有,需要重写它提供的一些方法才可以,有些则怎么都办不到,例如修改光标的宽度,如果哪位知道,请告诉我下,谢谢。

1. 修改光标的位置

    修改光标的位置也就是修改可编辑区域的位置,默认属性是没有,但是可以重写uiTextField,如下:

    MESearchTextField.h

#import <UIKit/UIKit.h>

@interface MESearchTextField : UITextField

@end

   MESearchTextField.m

#import "MESearchTextField.h"

@implementation MESearchTextField

// 控制placeHolder的位置,左右缩20,但是光标位置不变
/*
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+100, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
    return inset;
}
*/

// 修改文本展示区域,一般跟editingRectForBounds一起重写
- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-25, bounds.size.height);//更好理解些
    return inset;
}

// 重写来编辑区域,可以改变光标起始位置,以及光标最右到什么地方,placeHolder的位置也会改变
-(CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-25, bounds.size.height);//更好理解些
    return inset;
}

@end
  

通过上图可以看到,光标初始位置实际上跟输入框边距是x+10起了作用,然后你一直编辑,发现光标不会到最后,会停留在关闭按钮前面,这个是通过bounds.size.width-25来设置的。。。

二、uiTextField的其他属性

  // 搜索框
    _searchFieldText.backgroundColor = UIColorFromHex(0xDEDEDE);
    _searchFieldText.borderStyle = UITextBorderStyleNone;
    _searchFieldText.font = FontLarge;
    _searchFieldText.textColor = ColorTextLight;
    _searchFieldText.placeholder = @" 搜索";
    _searchFieldText.delegate = self;
    _searchFieldText.layer.cornerRadius = 2;
    _searchFieldText.returnKeyType = UIReturnKeySearch;  //设置按键类型
    _searchFieldText.enablesReturnKeyAutomatically = YES; //这里设置为无文字就灰色不可点
    _searchFieldText.clearButtonMode = UITextFieldViewModeWhileEditing; // 出现删除按钮
    // 设置占位文字的颜色为红色
    [_searchFieldText setValue:ColorTextLight forKeyPath:@"_placeholderLabel.textColor"];
    // 光标颜色
    _searchFieldText.tintColor = ColorTextLight;

三、重写的一些方法

– textRectForBounds:  //重写来重置文字区域
– drawTextInRect:    //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– placeholderRectForBounds:  //重写来重置占位符区域
– drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
– borderRectForBounds:  //重写来重置边缘区域
– editingRectForBounds:  //重写来重置编辑区域
– clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真
– leftViewRectForBounds:
– rightViewRectForBounds:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS UITextField光标大小和颜色是由系统控制的,我们不能直接修改其大小,但是可以通过修改 UITextField 的 tint 属性来改变光标的颜色。 虽然不能直接修改光标大小,但我们可以通过设置 UITextField 的边框样式为 None,然后通过添加一个自定义的 UIView 作为光标来模拟实现。 以下是一个示例代码,可以在 UITextField 添加一个自定义的光标: ```swift let customCursorView = UIView(frame: CGRect(x: 0, y: 0, width: 2, height: textField.frame.height)) customCursorView.backgroundColor = UIColor.red textField.tintColor = UIColor.clear textField.addSubview(customCursorView) ``` 在这个代码,我们创建了一个自定义的 UIView,用于模拟光标。我们将其宽度设置为 2,高度设置为 UITextField 的高度,颜色设置为红色,并将其添加到 UITextField 上。最后,我们将 UITextField 的 tint 设置为 clear,以隐藏系统的光标。 需要注意的是,为了保证自定义光标位置和系统光标位置一致,我们还需要在 UITextField 的代理方法添加以下代码: ```swift func textFieldDidChangeSelection(_ textField: UITextField) { if let selectedRange = textField.selectedTextRange { let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start) if let customCursorView = textField.subviews.last { customCursorView.frame.origin.x = textField.frame.origin.x + textField.textRect(forBounds: textField.bounds).origin.x + textField.font!.size(of: String(textField.text![..<textField.text!.index(textField.text!.startIndex, offsetBy: cursorPosition)]), constrainedToWidth: textField.frame.width).width } } } ``` 在这个代理方法,我们获取了 UITextField 被选的文本范围,然后计算了光标UITextField 位置,并将自定义光标位置进行了调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值