Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
UI label with italics?
-
@mikael If I'm allowed, you should add in your r.rich_text
"<oblique -0.3>oblique</oblique>",
-
@cvp, 😁, works for me.
-
@mikael Thanks, and now I 🤐 (sorry for the added work)
-
I used HTML-like syntax for the rich text definitions, since it is familiar and easy to parse with BeautifulSoup... But it is not especially joyful to write, especially since Pythonista has no native support for closing tags.
So, couple of things to reduce the amount of tags you need to write:
-
If you use a specific complex style in the label string a lot, subclass RichLabel and define custom tags for it. For example:
class MyRichLabel(RichLabel): custom = { 's': '<b><shadow green 0/></b>' }
Now, wherever you use the tag <s>, it is replaced by the above definition.
-
You can also define ready-to-use Label classes, where a certain format is applied by default:
class MyRichLabel(RichLabel): custom = { 's': '<b><shadow green 0/></b>' } default = '<c white><s/></c>'
The way RichLabel class is built (it is actually a ui.Label in the end), you can set any usual ui.Label attribute defaults at the same time:
class MyRichLabel(RichLabel): custom = { 's': '<b><shadow green 0/></b>' } default = '<c white><s/></c>' font = ('Arial', 24) alignment = ui.ALIGN_CENTER number_of_lines = 0
Now we can use it without extra tagging:
fancy = MyRichLabel( background_color='white', ) fancy.rich_text('FANCY BLOCK')
With the result:
-
In the best case you do not need to write any tags, if you just need one style and are happy with the defaults. Following label classes are defined in the package:
BoldLabel
,ItalicLabel
,BoldItalicLabel
,ObliqueLabel
,BoldObliqueLabel
OutlineLabel
UnderlineLabel
,StrikeLabel
ShadowLabel
BodyLabel
,CalloutLabel
,Caption1Label
,Caption2Label
,FootnoteLabel
,HeadlineLabel
,SubheadlineLabel
,LargeTitleLabel
,Title1Label
,Title2Label
,Title3Label
Due to limitations of the built-in view classes, you still need to use the
rich_text
method - good oldtext
will just give you good old plain text. -
-
@cvp, @peiriannydd and others, would appreciate your take on this.
Instead of the tag soup, it would be easy for me to implement the style formatting like this:
text = ''' Plain b: Bold & i: italic and & i: f system 32: just & italic f Zapfino: c red: Color shadow: Shadow u lightgrey: Outlines: b: o: DEFAULT o blue: COLORED o -3 + c orange: FILLED oblique: oblique strike double red byword + oblique: really not cool '''
White space after the colon is ignored.
&
at the end of the line indicates that there is no line break.Which format would you prefer? Of course both can be supported, but one would be the default.
-
@mikael said:
Which format would you prefer
Even if the new format is less talkative, I prefer the html-like format, more usual, but that's only a personal feeling.
-
-
@cvp, thanks, for both comments.
Also, I added your code as an
html
method that takes, well, some html. -
For reference, here’s also @peiriannydd’s original request in both markup options:
"लाल laal <i><c red>red</c></i>"
"""नीला neela & i + c blue: blue"""
Hmm, the latter might not be tag-verbose, i.e. more DRY, but it is ”line-verbose”.
-
-
Aaand the last idea for this evening, use different tag delimiters because they act as better vertical separators, and Pythonista editor nicely matches closing pairs. Combined with the idea of ad-hoc combined tag creation.
"लाल laal {i + c red}red{i}"
Hmm, does not work nicely with f-strings, and that is a clear requirement.
So:
"लाल laal [i + c red]red[i]"
-
-
@mikael I have enjoyed using your richlabels (thank you!!). One thing I haven’t figured out - When I try to change the font with
<f times new roman 15>
I get an error that it can't find a font named times. It seems like it is looking for the first word only in the font name. I don’t know python syntax well enough - is there a way I can write my code that will combine the whole three-word string when passed to the richlabels parser to have a multiword font, or is this a limitation of the richlabels module?
Thanks again!
-
@peiriannydd this works (case sensitive)
r = RichLabel( font=('Times New Roman', 24), background_color='white', alignment=ui.ALIGN_CENTER, number_of_lines=0, )
-
@cvp yes, but I was hoping to change the font within the label, and <f 'Times New Roman' 60> doesn't work. For example, this doesn't work:
r = RichLabel( font=('Chalkboard SE', 60), background_color='white', alignment=ui.ALIGN_CENTER, number_of_lines=0, ) r.rich_text("\n".join([ "First font", "<f 'Times New Roman' 60>Second font</f>, ]))
-
@peiriannydd, needed a little change in code, but now you can do:
<f Times-New-Roman 60>
Unfortunately I am just in the process of rearranging my repo. If you want this to work now, you can make a change to the file on line 85, I think:
Current:
self.font_name = node_font or self.font_name
Change to:
self.font_name = (node_font or self.font_name).replace('-', ' ')
-