【问题标题】:Color Text Background in GTK#GTK 中的彩色文本背景#
【发布时间】:2015-10-16 02:03:52
【问题描述】:

我正在使用 GTK# 和 TextWidget 来显示可编辑的文本。我希望每个文本项的背景颜色由字符确定(以便所有“A”都是红色,所有“G”都是绿色,所有“C”都是蓝色等)。

这似乎是可能的,但有人知道告诉 GTK# 以这种方式为输入文本着色的有效方法吗?

【问题讨论】:

    标签: mono gtk gtk#


    【解决方案1】:

    您可以使用 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 ();
    

    【讨论】:

    • 你知道我该如何响应用户添加文本吗?
    • 是的。 InsertWithTags 等效于 Insert 后跟 ApplyTag。我在源代码中添加了一个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 2012-08-22
    相关资源
    最近更新 更多