在 UITextView 中获取当前段落? [英] Get current paragraph in UITextView?

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

问题描述

我想检测当前段落,这是我目前的代码,但效果不佳.假设我有 3 个段落,当光标位于它们之间时,它会选择下一个,这是不对的.有没有更好的方法来做到这一点?

I want to detect the current paragraph, this is my code so far, but it doesn't work so well. Lets say I have 3 paragraphs and when the cursor is between them, it selects the next one, which is not right. Is there a better way to do this?

通过这段代码,我想检测当前段落,然后更改该段落的字体,然后继续使用该字体进行书写.

With this code, I want to detect the current paragraph, then change the font of that paragraph, and then continue writing with that font.

func textViewDidChangeSelection(textView: UITextView)
{

   // print("selected")
    //stylesDefaults()
    var arr = [String]()

    composeTextView.text.enumerateSubstringsInRange(Range(start: composeTextView.text.startIndex, end: composeTextView.text.endIndex), options: NSStringEnumerationOptions.ByParagraphs,
        { (substring, substringRange, enclosingRange, bool) -> () in arr.append(substring!)
    })

    if(composeTextView.text.characters.count != 0)
    {
        self.titleTextField.text = arr[0]
    }

    let currentRange : NSRange = composeTextView.selectedRange
    var charCounter : Int = 0
    var found : Bool = false
    for (var i = 0; i < arr.count; i++)
    {
        charCounter = charCounter + arr[i].characters.count
        if found == false
        {
            if(charCounter > currentRange.location)
            {
                print(arr[i])
                found = true
            }
        }

    }

}

推荐答案

听起来你需要 NSStringparagraphRangeForRange: 方法:

It sounds like you need NSString's paragraphRangeForRange: method:

let composeText = composeTextView.text as NSString
let paragraphRange = composeText.paragraphRangeForRange(composeTextView.selectedRange)
print(composeText.substringWithRange(paragraphRange))

这篇关于在 UITextView 中获取当前段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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