本文介绍了如何在Gtk :: TextView中设置可见行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gtk :: TextView,无论字体大小如何,我总是希望有两行可见的文本.显然,如果输入的行多于两行,则该框会滚动,但我希望文本视图保持两行高.

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.

我该怎么做?

推荐答案

这非常困难.例如,如果将两种字体大小混合在一行中怎么办?

This is very difficult. For example, what will you do if two font sizes are mixed in one line?

一种方法是创建一个字母的Pango布局并找出其高度.这是我一次用C编写的某些代码的未经测试的简化;但是将其转换为C ++和GTKmm应该没有太大麻烦:

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);

这篇关于如何在Gtk :: TextView中设置可见行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:41