Pango.Layout
Pango.Layout结构表示整个文本段落。一般使用Pango.Context来进行初始化,使用Pango.Layout.set_text(text, length)方法来设置文本;或者使用Widget.create_pango_layout(text)方法来创建,并设置该字符串的属性。
设置完成后,格式化的文本段落就可以从对象中获取,布局将会渲染呈现出来。
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (context) |
context_changed () | |
copy () | |
get_alignment () | |
get_attributes () | |
get_auto_dir () | |
get_baseline () | |
get_character_count () | |
get_context () | |
get_cursor_pos (index_) | |
get_ellipsize () | |
get_extents () | |
get_font_description () | |
get_height () | |
get_indent () | |
get_iter () | |
get_justify () | |
get_line (line) | |
get_line_count () | |
get_line_readonly (line) | |
get_lines () | |
get_lines_readonly () | |
get_log_attrs () | |
get_log_attrs_readonly () | |
get_pixel_extents () | |
get_pixel_size () | |
get_serial () | |
get_single_paragraph_mode () | |
get_size () | |
get_spacing () | |
get_tabs () | |
get_text () | |
get_unknown_glyphs_count () | |
get_width () | |
get_wrap () | |
index_to_line_x (index_, trailing) | |
index_to_pos (index_) | |
is_ellipsized () | |
is_wrapped () | |
move_cursor_visually (strong, old_index, old_trailing, direction) | |
set_alignment (alignment) | |
set_attributes (attrs) | |
set_auto_dir (auto_dir) | |
set_ellipsize (ellipsize) | |
set_font_description (desc) | |
set_height (height) | |
set_indent (indent) | |
set_justify (justify) | |
set_markup (markup, length) | |
set_markup_with_accel (markup, length, accel_marker) | |
set_single_paragraph_mode (setting) | |
set_spacing (spacing) | |
set_tabs (tabs) | |
set_text (text, length) | |
set_width (width) | |
set_wrap (wrap) | |
xy_to_index (x, y) |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/6
# section 140
TITLE = "Layout"
DESCRIPTION = """
Pango layout is an object representing a paragraph of text with attributes.
"""
import gi
gi.require_version("Gtk", "3.0")
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, Gdk, Pango, PangoCairo
import cairo
lyrics = """Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
You say why did you do it with him today?
and sniff me out like I was Tanqueray
cause you're my fella, my guy
hand me your stella and fly
by the time I'm out the door
you tear men down like Roger Moore
I cheated myself
like I knew I would
I told ya, I was trouble
you know that I'm no good"""
class Area(Gtk.DrawingArea):
def __init__(self):
super(Area, self).__init__()
self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(16400, 16400, 16440))
self.connect("draw", self.draw)
def draw(self, da, cr):
font_desc = Pango.FontDescription('Sans 15')
layout = da.create_pango_layout(lyrics)
layout.set_markup("<span foreground=\"#FF00FF\">%s</span>" % layout.get_text(), -1)
layout.set_width(Pango.SCALE * self.get_allocation().width)
layout.set_spacing(Pango.SCALE * 3)
layout.set_alignment(Pango.Alignment.CENTER)
layout.set_font_description(font_desc)
# PangoCairo.layout_path(cr, layout)
# cr.set_source_rgb(0.5, 0, 0)
# cr.fill()
PangoCairo.show_layout(cr, layout)
return True
class PyApp(Gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.connect("destroy", Gtk.main_quit)
self.set_title("You know I'm no Good")
self.add(Area())
self.set_size_request(300, 300)
self.move((self.get_screen().width() - 300) / 2, (self.get_screen().height() - 300) / 2)
self.show_all()
def main():
PyApp()
Gtk.main()
if __name__ == '__main__':
main()