2017-11-24 35 views
0

私のアプリケーションでRichTextEditorを作成しようとしています。Swiftの改行( n)でbackgroundColorを適用するUITextView.typingattributesを防止する

ユーザーは、編集中にフォアグラウンドカラーとバックグラウンドカラーの両方を変更できます。

は、ここで私はtypingAttributesを設定する方法は次のとおりです。

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

    self.setTypingColor(foregroundColor: self.foregroundColor, backgroundColor: self.backgroundColor) 

    return true 
} 

しかし、私は、ユーザーが\nを入力すれば、編集者はまた、行全体に背景色を適用することがわかりました。

enter image description here

は、ここで私はそれになりたいものです。ある

enter image description here

は、私はそれは文字だけではなく、\n\t\rにこれらの色を適用します。

自分自身でAttributedStringを作成せずに行う方法はありますか?

答えて

1

単純なケースでは、このようなことが働く可能性があります。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { 
    if text == "\n" { // better matching required 
     self.setTypingColor(foregroundColor: .clear, backgroundColor: .clear) 
    } else { 
     self.setTypingColor(foregroundColor: self.foregroundColor, backgroundColor: self.backgroundColor) 
    } 
    return true 
} 

しかし、ユーザーは、私はあなたが起因した文字列を扱う起動する必要がありかもしれないと思う、その場合には、あなたのテキストビューに文字列を貼り付けたときに、問題はおそらく持続します。

+0

これは私が心配していることです。ユーザーは、Webページ、メモなどのテキストを貼り付けることができます。色付けの振る舞いを変えることができる財産はありませんか? –

+0

私は財産を知らない。私がこれを行うと考えることができる唯一の方法は、アトリビュートされた文字列を手動で編集することです。 – Pulsar

+0

よろしくお願いいたします。あなたの答えは、 'text.count == 1'が真である間は依然として有用です。 –

関連する問題