在UITextView.text中使用Swift 2.2,iOS 9.3和Xcode 7修改单词并使用前缀修改单词的方法?
使用TextView进行用户消息输入时,它可在用户编辑期间进行编辑和选择。 字段下面的按钮之一,在编辑和散列标记模式之间切换TextView。 切换到标记时,TextView有可编辑的&可选属性禁用,并且我有一个函数来检测轻敲并返回文本内的字符位置。在UITextView.text中使用Swift 2.2,iOS 9.3和Xcode 7修改单词并使用前缀修改单词的方法?
我需要确定单词,如果有的话,点击并修改UITextView.text中的单词前加一个#,除非它已经有两个哈希值,它删除了哈希值。 我正在使用逻辑的正则表达式。
我一直没有找到一个高级别的方法来确定点击字符的单词。 我已经通过苹果的开发搜索。库。和像raywenderlich和Grok Swift这样的网站,但找不到我确定的方法必须在那里。
我可以通过测试来实现当前字符是否是有效的字词分隔符,如果不是,则递减字符索引并测试,直到确定字词边界。在这一点上,我返回到先前的索引并测试#字符,如果它是#,我会测试下一个字符,如果不是#,我会将#字符添加到开头的词。
在UIKit,TextKit或UITextView或NSTextStorage的方法中是否有一个函数,它会返回点击字符的单词和该单词的NSRange? 另外,将#添加到TextView文本的正确方法是什么? [TextView的:shouldChangeTextInRange:replacementText或textView.textStorage:replaceCharactersInRange:withString:]
我已作商业上的PC,游戏机和GameBoy的,但是这是第一次开发的应用程序,并使用iPhone/Mac平台,所以我真的可以使用建议。
检测#你需要调用的代码委托shouldChangeCharactersInRange
let stringprocess = stringfordetecting.text
let tok = stringprocess!.componentsSeparatedByString(" ")
for item in tok
{
let demo = String(item)
if demo.hasPrefix("#")
{
let range = (stringfordetecting.text! as NSString).rangeOfString(item)
//add code
}
else
{
//add code
}
内检测抽头字符指数的手势语添加到TextView的
let tapGesture = UITapGestureRecognizer(target: self, action: "textTapped:")
tapGesture.headline = indexPath
tapGesture.numberOfTapsRequired = 1
textview2.addGestureRecognizer(tapGesture)
func textTapped(recognizer: MyTapGestureRecognizer){
let textView: UITextView = recognizer.view as! UITextView
var layoutManager: NSLayoutManager = textView.layoutManager
var location: CGPoint = recognizer.locationInView(textView)
let position: CGPoint = CGPointMake(location.x, location.y)
location.x -= textview2.textContainerInset.left
location.y -= textview2.textContainerInset.top
var charIndex: Int
charIndex = layoutManager.characterIndexForPoint(location, inTextContainer: textview2.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if charIndex < textview2.textStorage.length
{
print(charIndex)
}
}
为检测抽头识别器功能内的文本视图中的轻敲字符
func textTapped(recognizer: MyTapGestureRecognizer){
let textView: UITextView = recognizer.view as! UITextView
var layoutManager: NSLayoutManager = textView.layoutManager
var location: CGPoint = recognizer.locationInView(textView)
let position: CGPoint = CGPointMake(location.x, location.y)
location.x -= cell.messageLabel.textContainerInset.left
location.y -= cell.messageLabel.textContainerInset.top
var charIndex: Int
charIndex = layoutManager.characterIndexForPoint(location, inTextContainer: cell.messageLabel.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if charIndex < cell.messageLabel.textStorage.length {
let stringprocess = textview.text
let tok = stringprocess.componentsSeparatedByString(" ")
// let attributedString1 = NSMutableAttributedString(string:stringcheck as String)
for item in tok
{
let demo = String(item)
if demo.hasPrefix("@") {
let range = (stringcheck as NSString).rangeOfString(item)
var i = range.location
while i <= range.location+range.length
{
if i == charIndex
{
print(demo)
}
i++
}
}
}
}