9

I can't understand how to load text into a GtkTexView, how is it done?

Braiam
  • 1
  • 11
  • 47
  • 78
stighy
  • 7,260
  • 25
  • 97
  • 157

1 Answers1

23

You have to access the Buffer property that represents the buffer holding all the content that is shown by GtkTextView.

To simply load a text, you must set the Text property, like that:

textview1.Buffer.Text = "Some sample text that will be displayed."

Assuming the control you added has the name textview1.

If you want some more control on the apperance of the text, you have to use tags to mark the text. For example:

var tag = new TextTag (null);
this.textview1.Buffer.TagTable.Add (tag);
tag.Weight = Pango.Weight.Bold;
var iter = this.textview1.Buffer.GetIterAtLine (0);
this.textview1.Buffer.InsertWithTags (ref iter, "Bold text\n", tag);

This will insert a bold text in first line. A lot more is possible using the TextBuffer, look on the methods available on textview1.Buffer.

silk
  • 2,714
  • 22
  • 21
  • Thanks, this helped me. Do you happen to know why this, particularly the second part of your answer, gives a warning in Xamarin/Mono? Here is the warning: Warning CS0618: 'Gtk.TextBuffer.Insert(Gtk.TextIter, string)' is obsolete: 'Replaced by 'ref TextIter iter' overload' (CS0618) – Torchify Jun 01 '15 at 02:03