Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 键入时文本视图中的AttributedText_Ios_Swift_Uitextview_Nsattributedstring_Uitextviewdelegate - Fatal编程技术网

Ios 键入时文本视图中的AttributedText

Ios 键入时文本视图中的AttributedText,ios,swift,uitextview,nsattributedstring,uitextviewdelegate,Ios,Swift,Uitextview,Nsattributedstring,Uitextviewdelegate,我有一个文本视图,我试图给它一个属性文本。我试图在shouldChangeTextInRange中实现它,但它在超出索引范围时崩溃 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if myTextView { textView.attributedText = addAttribute

我有一个文本视图,我试图给它一个属性文本。我试图在
shouldChangeTextInRange
中实现它,但它在超出索引范围时崩溃

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { 

   if myTextView {
      textView.attributedText = addAttributedText(1, text: text, fontsize: 13)

      let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
      let numberOfChars = newText.characters.count

      return numberOfChars < 20
   }
   return true
}

 func addAttributedText(spacing:CGFloat, text:String, fontsize: CGFloat) -> NSMutableAttributedString {
    let attributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName:UIFont(
        name: "Font",
        size: fontsize)!])
    attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSMakeRange(0, text.characters.count))
    return attributedString

}
func textView(textView:UITextView,shouldChangeTextInRange:NSRange,replacementText:String)->Bool{
如果是myTextView{
textView.attributedText=addAttributedText(1,text:text,fontsize:13)
让newText=(textView.text作为NSString)。StringByRePlacingCharactersRange(范围,带字符串:text)
设numberOfChars=newText.characters.count
返回字符数<20
}
返回真值
}
func addAttributedText(间距:CGFloat,文本:String,fontsize:CGFloat)->NSMutableAttributedString{
让attributedString=NSMutableAttributedString(字符串:文本,属性:[NSFontAttributeName:UIFont(
名称:“字体”,
大小:fontsize)!])
attributedString.addAttribute(NSKernAttributeName,值:空格,范围:NSMakeRange(0,text.characters.count))
返回AttributeString
}
我尝试将带有空文本的attributedString添加到viewDidLoad中的textView,但没有帮助。这就是为什么我认为在
shouldChangeTextInRange

(请注意,我的
addAttributeText
方法对于其他文本视图非常有效)


如果我使用这个,在一个字符中输入,它会写2倍并崩溃。将textView的文本转换为正在键入的属性文本的正确方法是什么。

以下是我试图从上面的链接转换的代码,它可能有错误,但我希望它能够帮助您

func formatTextInTextView(textView: UITextView) 
{
    textView.scrollEnabled = false
    var selectedRange: NSRange = textView.selectedRange
    var text: String = textView.text!
    // This will give me an attributedString with the base text-style
    var attributedString: NSMutableAttributedString = NSMutableAttributedString(string: text)
    var error: NSError? = nil
    var regex: NSRegularExpression = NSRegularExpression.regularExpressionWithPattern("#(\\w+)", options: 0, error: error!)
    var matches: [AnyObject] = regex.matchesInString(text, options: 0, range: NSMakeRange(0, text.length))

    for match: NSTextCheckingResult in matches {
        var matchRange: NSRange = match.rangeAtIndex(0)
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: matchRange)
    }
    textView.attributedText = attributedString
    textView.selectedRange = selectedRange
    textView.scrollEnabled = true
}

编辑:没有看到在原始帖子中有一个快速的答案,这是链接:

你看过这里吗@EugeneZhenyaGordin不幸的是,这种类型的objective-c似乎让我感到困惑,因为我没有任何先验知识。你能帮忙吗?这里有一个快速版本:它工作得非常好!!!很高兴它有帮助@EugeneZhenyaGordin这个答案是剽窃的,没有任何归属。不要把别人的答案当作你自己的。首先,我把它的链接贴上去了。senty问我是否可以转换它,我确实使用了以下链接:。senty实际上找到了你稍后提到的swift版本,它在评论中。我的答案被标记为答案,我有罪吗?我可以在这篇文章中添加一个参考资料,没问题