如何将光标移动到UITextView中当前行的开头?

时间:2021-07-03 22:56:12

I want to have an insert tab button for my UITextView so that users be able to insert a tab at beginning of the current line. No matter where the cursor is in the line, the tab should be inserted at the beginning of the line, and after that the cursor must go to the end of the line.

我想为我的UITextView设置一个insert选项卡按钮,这样用户就可以在当前行开头插入一个选项卡。无论光标在行中何处,都应该在行开头插入选项卡,然后光标必须到达行尾。

Is it possible ?

是可能的吗?

3 个解决方案

#1


2  

UITextView implements the UITextInput protocol, which has a whole bunch of methods for determining positions of text. I'm not sure about this, but perhaps you can call:

UITextView实现了UITextInput协议,它有一大堆确定文本位置的方法。我不太确定,但是也许你可以打电话:

  1. selectedTextRange to get the caret position.
  2. 选择textrange以获得插入符号位置。
  3. positionFromPosition:inDirection:offset: with UITextLayoutDirectionLeft to find the range to the start of the current line. (This I'm not sure about; maybe characterRangeByExtendingPosition:inDirection: would work better?)
  4. 位置:间接:偏移:用UITextLayoutDirectionLeft找到当前行开始的范围。(我不确定;也许角色通过扩展位置:间接:会更好吗?
  5. Use textRangeFromPosition:toPosition: to get a range for the start of the lin.
  6. 使用textRangeFromPosition:toPosition:获取lin开始时的范围。
  7. Use replaceRange:withText: to insert a tab.
  8. 使用replaceRange:withText:插入一个选项卡。

There might be other methods in that protocol that would let you figure that out if that doesn't work.

该协议中可能还有其他方法可以让你知道,如果这不起作用。

Edit: Seems that UITextView only implements UITextInput as of iOS 5. If you're targeting before that, I'm not sure what to suggest.

编辑:看起来UITextView只实现了ios5的UITextInput。如果你的目标是在那之前,我不知道该建议什么。

#2


2  

I have no luck with positionFromPosition:inDirection:offset: and characterRangeByExtendingPosition:inDirection:.

我对位置不感兴趣:间接的,间接的,间接的,间接的。

But this works fine for me:

但这对我来说很好:

UITextRange *range = [textView selectedTextRange];
CGRect rect = [textView caretRectForPosition:range.start];
UITextPosition *start = [textView closestPositionToPoint:CGPointMake(0, rect.origin.y)];
[textView setSelectedTextRange:[textView textRangeFromPosition:start toPosition:start]];

UPD: the code above is tested on iOS7 and earlier. And can no longer work with iOS9. Actually, I have some bugs in my text-editing app running on iOS9-devices. I'll update my answer after fixing my app.

UPD:上面的代码在iOS7和更早的版本中进行了测试。并且不能再使用iOS9。实际上,我在ios9设备上运行的文本编辑应用程序中有一些错误。我修好我的应用后会更新我的答案。

#3


0  

I'll add the code that I've been using to move the cursor to the beginning of the line (homeTap) as it addresses some of the comments within @zxcat's answer, regarding the cursor being moved to the beginning of previous line rather than the current line:

我将添加我一直用来将光标移动到行的开头(家园地图)的代码,因为它处理了@zxcat的答案中关于光标移动到前一行而不是当前行的一些注释:

- (void)homeTap:(id)s {
    UITextRange* range = [_textView selectedTextRange];
    CGRect rect = [_textView caretRectForPosition:range.start];
    float halfLineHeight = _textView.font.lineHeight / 2.0;
    UITextPosition* start = [_textView closestPositionToPoint:CGPointMake(0, rect.origin.y + halfLineHeight)];
    [_textView setSelectedTextRange:[_textView textRangeFromPosition:start toPosition:start]];
}

Specifically, I add halfLineHeight to rect.origin.y to make sure the result from closestPositionToPoint is for the current line.

具体地说,我在rect.origin中加入了halfLineHeight。确保closestPositionToPoint的结果是针对当前行。

#1


2  

UITextView implements the UITextInput protocol, which has a whole bunch of methods for determining positions of text. I'm not sure about this, but perhaps you can call:

UITextView实现了UITextInput协议,它有一大堆确定文本位置的方法。我不太确定,但是也许你可以打电话:

  1. selectedTextRange to get the caret position.
  2. 选择textrange以获得插入符号位置。
  3. positionFromPosition:inDirection:offset: with UITextLayoutDirectionLeft to find the range to the start of the current line. (This I'm not sure about; maybe characterRangeByExtendingPosition:inDirection: would work better?)
  4. 位置:间接:偏移:用UITextLayoutDirectionLeft找到当前行开始的范围。(我不确定;也许角色通过扩展位置:间接:会更好吗?
  5. Use textRangeFromPosition:toPosition: to get a range for the start of the lin.
  6. 使用textRangeFromPosition:toPosition:获取lin开始时的范围。
  7. Use replaceRange:withText: to insert a tab.
  8. 使用replaceRange:withText:插入一个选项卡。

There might be other methods in that protocol that would let you figure that out if that doesn't work.

该协议中可能还有其他方法可以让你知道,如果这不起作用。

Edit: Seems that UITextView only implements UITextInput as of iOS 5. If you're targeting before that, I'm not sure what to suggest.

编辑:看起来UITextView只实现了ios5的UITextInput。如果你的目标是在那之前,我不知道该建议什么。

#2


2  

I have no luck with positionFromPosition:inDirection:offset: and characterRangeByExtendingPosition:inDirection:.

我对位置不感兴趣:间接的,间接的,间接的,间接的。

But this works fine for me:

但这对我来说很好:

UITextRange *range = [textView selectedTextRange];
CGRect rect = [textView caretRectForPosition:range.start];
UITextPosition *start = [textView closestPositionToPoint:CGPointMake(0, rect.origin.y)];
[textView setSelectedTextRange:[textView textRangeFromPosition:start toPosition:start]];

UPD: the code above is tested on iOS7 and earlier. And can no longer work with iOS9. Actually, I have some bugs in my text-editing app running on iOS9-devices. I'll update my answer after fixing my app.

UPD:上面的代码在iOS7和更早的版本中进行了测试。并且不能再使用iOS9。实际上,我在ios9设备上运行的文本编辑应用程序中有一些错误。我修好我的应用后会更新我的答案。

#3


0  

I'll add the code that I've been using to move the cursor to the beginning of the line (homeTap) as it addresses some of the comments within @zxcat's answer, regarding the cursor being moved to the beginning of previous line rather than the current line:

我将添加我一直用来将光标移动到行的开头(家园地图)的代码,因为它处理了@zxcat的答案中关于光标移动到前一行而不是当前行的一些注释:

- (void)homeTap:(id)s {
    UITextRange* range = [_textView selectedTextRange];
    CGRect rect = [_textView caretRectForPosition:range.start];
    float halfLineHeight = _textView.font.lineHeight / 2.0;
    UITextPosition* start = [_textView closestPositionToPoint:CGPointMake(0, rect.origin.y + halfLineHeight)];
    [_textView setSelectedTextRange:[_textView textRangeFromPosition:start toPosition:start]];
}

Specifically, I add halfLineHeight to rect.origin.y to make sure the result from closestPositionToPoint is for the current line.

具体地说,我在rect.origin中加入了halfLineHeight。确保closestPositionToPoint的结果是针对当前行。