×
Namespaces

Variants
Actions

Archived:How to specify text font in PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Tested with
Devices(s): Nokia N96
Compatibility
Platform(s): S60 2nd Edition, S60 3rd Edition
Article
Keywords: appuifw, text
Created: cyke64 (29 Mar 2007)
Last edited: hamishwillee (31 May 2013)

Contents

Overview

The appuifw Text widget allows to use rich text. However, it's quite difficult to use. You need to set each attribute (font, style, highlight, color) of the widget before adding more text with different style and also change all attributes for different styles.

So following function make it a bit easier. The font tag is used (borrowed from HTML).

Attributes allowed are :

  • Color: #RRGGBB or 0xRRGGBB or color name
  • Face: both font and size eg. albi17b )
  • Style: bold, italic, underline or strikethrough(styles can be combined using comma)
  • Highlight: standard, rounded, shadow)
  • Hcolor: highlight color

Code

Text attribute function

from appuifw import *
import appuifw
 
def process_color(color):
color_name = {
'red': 0xff0000, 'green': 0x008000, 'blue':0x0000ff,
'black': 0, 'white':0xffffff, 'yellow': 0xffff00
}
if color.startswith('#'): # HTML format #000000
return int(color[1:], 16)
if color.startswith('0x'): # pys60 format 0x000000
return int(color, 16)
return color_name[color]
 
def set_ml(t, s):
stack = []
t.clear()
t.font = 'normal'
i = 0
while i < len(s):
if s.startswith('<', i): # tag end or tag begin
j = s.find('>', i) + 1
if s.startswith('</font>', i) or s.startswith('</>', i):
t.color, t.font, t.style, t.highlight_color = stack.pop()
else:
stack.append([t.color, t.font, t.style, t.highlight_color])
to_style = 0
for attr_val in s[i:j-1].split(' '):
if '=' in attr_val:
attr, val = attr_val.split('=')
if attr == 'color':
t.color = process_color(val)
elif attr == 'face':
t.font = unicode(val)
elif attr == 'hcolor':
t.highlight_color = process_color(val)
elif attr == 'style': # style and highlight go together
for st in val.split(','):
to_style |= getattr(appuifw, 'STYLE_' + st.upper())
elif attr == 'highlight':
to_style |= getattr(appuifw, "HIGHLIGHT_" + val.upper())
if to_style:
t.style = to_style
else: # normal text
j = s.find('<', i)
if j == -1: j = len(s)
text = u'' + s[i:j].replace('&lt;', '<')
t.add(text)
i = j # go next chunk

Example Usage

# import modules
from appuifw import *
import e32, apuuifw
# define quit function
def quit():
a.signal()
appuifw.app.exit_key_handler = quit
 
t = app.body # use the default Text widget that start pys60
# write text "Hello World"
set_ml(t, '<font color=red>Hello</font> <font style=bold>World</font>.')
 
a = e32.Ao_lock()
a.wait() # wait for exit

Postconditions

Following screenshot shows the result of above code.

Stylish Hello World text

Related Link

Archived:How to use Text in PySymbian

This page was last modified on 31 May 2013, at 04:09.
47 page views in the last 30 days.
×