如何在UITextView中将光标移动到当前行的开头? [英] How to move cursor to the beginning of the current line in UITextView?

查看:166
本文介绍了如何在UITextView中将光标移动到当前行的开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为UITextView设置一个插入标签按钮,以便用户能够在当前行的开头插入一个标签。
不管光标在行中的何处,都应在行的开头插入制表符,然后光标必须移至行的末尾。

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.

有可能吗?

推荐答案

我对 positionFromPosition:inDirection:offset没有好运: characterRangeByExtendingPosition:inDirection:

但这对我来说很好:

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:以上代码已在iOS7及更早版本上进行了测试。并且无法再与iOS9一起使用。实际上,我在iOS9设备上运行的文本编辑应用程序中存在一些错误。修复应用后,我将更新我的答案。

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.

这篇关于如何在UITextView中将光标移动到当前行的开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆