问题
I have a Gtk::TextView that I would always like to have two lines of text visible, regardless of the font size. Obviously if more than two lines were entered then the box would scroll but I'd like the text view to remain 2 lines tall.
How do I do this?
回答1:
This is very difficult. For example, what will you do if two font sizes are mixed in one line?
One way to do it is to create a Pango layout of one letter and find out its height. This is an untested simplification of some code I wrote in C one time; but it shouldn't be too much trouble to convert it to C++ and GTKmm:
PangoLayout *cell = gtk_widget_create_pango_layout(textview, "X");
int line_height;
pango_layout_get_pixel_extents(cell, NULL, &line_height);
g_object_unref(cell);
gtk_widget_set_size_request(textview, -1, line_height);
来源:https://stackoverflow.com/questions/5631486/how-do-i-set-the-number-of-visible-lines-in-a-gtktextview