UITextView获取选定的文本点 [英] UITextView get selected text point

查看:62
本文介绍了UITextView获取选定的文本点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有什么方法可以在UITextView中获得所选文本的重点吗?

I was wondering is there any way to get the point of the selected text in UITextView ?

谢谢!

推荐答案

对于iOS5及更高版本:现在UITextFieldUITextView符合UITextInput协议,因此有可能:)

For iOS5 and above : Now UITextField and UITextView conform to UITextInput protocol so it is possible :)

选择插入符号前的最后5个字符将是这样的:

Selecting the last 5 characters before the caret would be like this:

//Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];

NSLog("Start: %d <> End: %d", selectedRange.start, selectedRange.end);

//Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5];

//Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];

这篇关于UITextView获取选定的文本点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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