【发布时间】:2015-10-16 02:03:52
【问题描述】:
我正在使用 GTK# 和 TextWidget 来显示可编辑的文本。我希望每个文本项的背景颜色由字符确定(以便所有“A”都是红色,所有“G”都是绿色,所有“C”都是蓝色等)。
这似乎是可能的,但有人知道告诉 GTK# 以这种方式为输入文本着色的有效方法吗?
【问题讨论】:
我正在使用 GTK# 和 TextWidget 来显示可编辑的文本。我希望每个文本项的背景颜色由字符确定(以便所有“A”都是红色,所有“G”都是绿色,所有“C”都是蓝色等)。
这似乎是可能的,但有人知道告诉 GTK# 以这种方式为输入文本着色的有效方法吗?
【问题讨论】:
您可以使用 TextTag 更改 Gtk.TextView 中文本的颜色。
下面的示例创建了一个错误标记,它在插入文本时以红色背景突出显示文本。
var textView = new Gtk.TextView ();
var errorTag = new TextTag ("error");
errorTag.Background = "#dc3122";
errorTag.Foreground = "white";
errorTag.Weight = Pango.Weight.Bold;
textView.Buffer.TagTable.Add (errorTag);
string text = "foo";
// Insert text with tag.
TextIter start = textView.Buffer.EndIter;
textView.Buffer.InsertWithTags (ref start, text, errorTag);
text = "bar";
// Insert text then apply tag.
textView.Buffer.Insert (ref start, text);
start = textView.Buffer.GetIterAtOffset (5);
TextIter end = textView.Buffer.GetIterAtOffset (6);
textView.Buffer.ApplyTag (errorTag, start, end);
var vbox = new Gtk.VBox ();
Add (vbox);
vbox.PackStart (textView);
vbox.ShowAll ();
【讨论】: