简体   繁体   English

如何设置UITextField的自定义光标位置?

[英]How to set custom cursor position of UITextField?

When user clicks on UITextFiled the cursor is at starting point, for design purpose its not look good. 当用户单击UITextFiled时,光标位于起点,出于设计目的,它看起来并不好。 So, I want to change postion of cursor +5 or from center of UITextField. 因此,我想更改光标+5或从UITextField的中心位置。

I had implemented this code found from Github, Stil its not working. 我已经实现了从Github找到的这段代码,Stil无法正常工作。 Please help me if you can guide me how to solve this problem. 如果您可以指导我如何解决此问题,请帮助我。

My code is below 我的代码如下

#import "UITextField+Selection.h"

@implementation UITextField (Selection)

- (NSRange)selectedRange
{
    UITextPosition* beginning = self.beginningOfDocument;
    UITextRange* selectedRange = self.selectedTextRange;
    UITextPosition* selectionStart = selectedRange.start;
    UITextPosition* selectionEnd = selectedRange.end;
    NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
    NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
    return NSMakeRange(location, length);
}
- (void)setSelectedRange:(NSRange)range
{
    UITextPosition* beginning = self.beginningOfDocument;
    UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
    UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
    UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
    [self setSelectedTextRange:selectionRange];
}

#import "UITextField+Selection.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

   [self.nameTextField setSelectedRange:NSMakeRange(5,0)]; // Category method called
}
@end

Instead of moving the cursor you can add padding to the left of the UITextFiled like this for design to look better 代替移动光标,您可以像这样在UITextFiled的左侧添加填充以使设计看起来更好

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

If you want to have custom position of cursor when user selects UITextField you need to register for notification that is posted when user selects UITextField . 如果要在用户选择UITextField时具有自定义光标的位置,则需要注册用户选择UITextField时发布的通知。

So you can add yourself as observer (for example in viewDidLoad method) via: 因此,您可以通过以下方式将自己添加为观察者(例如,在viewDidLoad方法中):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldSelected) name:UITextFieldTextDidBeginEditingNotification object:nil];

And then in textFieldSelected method you can: 然后在textFieldSelected方法中,您可以:

- (void)textFieldSelected {
    [self.nameTextField setSelectedRange:NSMakeRange(5,0)];
}

For designing purpose, You can put Image behind UITextview, and put textview above UIImage view. 出于设计目的,您可以将Image放在UITextview后面,并将textview放在UIImage视图上方。 It will look good for design 看起来很适合设计

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM