mirror of https://github.com/kivy/kivy.git
Merge branch 'label_lineheight' of https://github.com/hansent/kivy into hansent-label_lineheight
This commit is contained in:
commit
17bf207703
|
@ -0,0 +1,34 @@
|
|||
|
||||
'''
|
||||
Label textsize
|
||||
============
|
||||
|
||||
This example shows how the textsize and line_height property are used
|
||||
to format label widget
|
||||
'''
|
||||
|
||||
import kivy
|
||||
kivy.require('1.0.7')
|
||||
|
||||
from kivy.app import App
|
||||
from kivy.uix.label import Label
|
||||
|
||||
|
||||
_long_text = """
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, pellentesque molestie adipiscing vitae, aliquam at tellus. Fusce quis est ornare erat pulvinar elementum ut sed felis. Donec vel neque mauris. In sit amet nunc sit amet diam dapibus lacinia. In sodales placerat mauris, ut euismod augue laoreet at. Integer in neque non odio fermentum volutpat nec nec nulla. Donec et risus non mi viverra posuere. Phasellus cursus augue purus, eget volutpat leo. Phasellus sed dui vitae ipsum mattis facilisis vehicula eu justo.
|
||||
|
||||
Quisque neque dolor, egestas sed venenatis eget, porta id ipsum. Ut faucibus, massa vitae imperdiet rutrum, sem dolor rhoncus magna, non lacinia nulla risus non dui. Nulla sit amet risus orci. Nunc libero justo, interdum eu pulvinar vel, pulvinar et lectus. Phasellus sed luctus diam. Pellentesque non feugiat dolor. Cras at dolor velit, gravida congue velit. Aliquam erat volutpat. Nullam eu nunc dui, quis sagittis dolor. Ut nec dui eget odio pulvinar placerat. Pellentesque mi metus, tristique et placerat ac, pulvinar vel quam. Nam blandit magna a urna imperdiet molestie. Nullam ut nisi eget enim laoreet sodales sit amet a felis.
|
||||
"""
|
||||
|
||||
|
||||
class LabelTextSizeTest(App):
|
||||
def build(self):
|
||||
l = Label(
|
||||
text=_long_text,
|
||||
text_size = (600,None),
|
||||
line_height=1.5
|
||||
)
|
||||
return l
|
||||
|
||||
if __name__ == '__main__':
|
||||
LabelTextSizeTest().run()
|
|
@ -84,12 +84,12 @@ class LabelBase(object):
|
|||
def __init__(self, text='', font_size=12, font_name=DEFAULT_FONT,
|
||||
bold=False, italic=False, halign='left', valign='bottom',
|
||||
shorten=False, text_size=None, mipmap=False, color=None,
|
||||
**kwargs):
|
||||
line_height=1.0, **kwargs):
|
||||
|
||||
options = {'text': text, 'font_size': font_size,
|
||||
'font_name': font_name, 'bold': bold, 'italic': italic,
|
||||
'halign': halign, 'valign': valign, 'shorten': shorten,
|
||||
'mipmap': mipmap}
|
||||
'mipmap': mipmap, 'line_height': line_height}
|
||||
|
||||
options['color'] = color or (1, 1, 1, 1)
|
||||
options['padding'] = kwargs.get('padding', 0)
|
||||
|
@ -245,6 +245,7 @@ class LabelBase(object):
|
|||
if uw is None:
|
||||
for line in self.text.split('\n'):
|
||||
lw, lh = get_extents(line)
|
||||
lh = lh * options['line_height']
|
||||
if real:
|
||||
x = 0
|
||||
if halign == 'center':
|
||||
|
@ -292,6 +293,7 @@ class LabelBase(object):
|
|||
gw, gh = cache[glyph]
|
||||
ww += gw
|
||||
wh = max(gh, wh)
|
||||
wh = wh * options['line_height']
|
||||
|
||||
# is the word fit on the uw ?
|
||||
if ww > uw:
|
||||
|
@ -301,7 +303,6 @@ class LabelBase(object):
|
|||
|
||||
# get the maximum height for this line
|
||||
lh = max(wh, lh)
|
||||
|
||||
# is the word fit on the line ?
|
||||
if (word == '\n' or x + ww > uw) and lw != 0:
|
||||
# no, push actuals glyph
|
||||
|
|
|
@ -222,7 +222,7 @@ class MarkupLabel(MarkupLabelBase):
|
|||
uw, uh = self.text_size
|
||||
|
||||
# split the word
|
||||
default_line_height = get_extents(' ')[1]
|
||||
default_line_height = get_extents(' ')[1] * self.options['line_height']
|
||||
for part in re.split(r'( |\n)', word):
|
||||
|
||||
if part == '':
|
||||
|
@ -244,7 +244,7 @@ class MarkupLabel(MarkupLabelBase):
|
|||
pg = [cache[g] for g in part]
|
||||
pw = get_extents(part)[0]
|
||||
ph = max([g[1] for g in pg])
|
||||
|
||||
ph = ph * self.options['line_height']
|
||||
options = copy(options)
|
||||
|
||||
# check if the part can be put in the line
|
||||
|
|
|
@ -117,7 +117,7 @@ class Label(Widget):
|
|||
|
||||
_font_properties = ('text', 'font_size', 'font_name', 'bold', 'italic',
|
||||
'halign', 'valign', 'padding_x', 'padding_y', 'text_size', 'shorten',
|
||||
'mipmap', 'markup')
|
||||
'mipmap', 'markup', 'line_height')
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self._trigger_texture = Clock.create_trigger(self.texture_update, -1)
|
||||
|
@ -284,6 +284,16 @@ class Label(Widget):
|
|||
12dp.
|
||||
'''
|
||||
|
||||
line_height = NumericProperty(1.0)
|
||||
'''Line Height for the text. e.g. line_height = 2 will cause the spacing
|
||||
between lines to be twice the size.
|
||||
|
||||
:data:`line_height` is a :class:`~kivy.properties.NumericProperty`, default to
|
||||
1.0.
|
||||
'''
|
||||
|
||||
|
||||
|
||||
bold = BooleanProperty(False)
|
||||
'''Indicates use of the bold version of your font.
|
||||
|
||||
|
|
Loading…
Reference in New Issue