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?
-
@cvp, ooo, cool. The solution that ends all solutions.
... Although, there are several things I do not like about HTML styling and layouts, so I might still use the other option if I need rich text in my labels.
One thing I meant to ask: what does the objc_ptr do? Code seems to work without it.
-
-
@mikael said:
there are several things I do not like about HTML styling and layouts, so I might still use the other option if I need rich text in my labels.
I agree
-
@mikael You could also add NSStrikethroughColor
I've modified your code only for testing, not Python's way
. . . def apply(self, attr_str): attr_str.addAttribute_value_range_( objc_util.ns(self.attr_key), self.line_style, objc_util.NSRange(self.start, self.end - self.start)) self.through_color = (1, 0, 0, 1) self.objc_color = objc_util.UIColor.colorWithRed_green_blue_alpha_( *self.through_color) attr_str.addAttribute_value_range_( objc_util.ns('NSStrikethroughColor'), self.objc_color, objc_util.NSRange(self.start, self.end - self.start)) . . .
-
-
@cvp said:
NSStrikethroughColor
Of course lines must have colors! Now available as additional qualifier for the line tags:
"Plain", "<b>Bold <i>italic</i></b>", "and <i><f system 32>just</f> italic</i>", "", "<f Zapfino><c red>Color</c>", "<shadow>Shadow</shadow></f>", "<c white><shadow green 0><b>BLOCK</b></shadow></c>", "", "<u lightgrey>Outlines:</u>", "<b>", "<o>DEFAULT</o>", "<o blue>COLORED</o>", "<o -3><c orange>FILLED</c></o>", "</b>", "<strike double red byword>really not cool</s>"
Result:
-
@mikael and this one?
self.attr_str.addAttribute_value_range_( objc_util.ns('NSObliqueness'), ns(-0.8), # <0: left >0: right objc_util.NSRange(self.start, self.end - self.start))
-
This post is deleted! -
@cvp, 😏 <— see the oblique smile
Added the
oblique
tag with a single float parameter. Default is 0.25, which roughly corresponds to italic on iOS.(Not adding a picture since you already did.)
-
@mikael not yet in the usual github?
-
@cvp, you are fast! I changed the default, as described above, now committed.
-
@mikael whaaaa, you've added new tags....
-
@cvp, yes, these tags use the standard iOS styles, if you want your UI to follow Apple style guidelines:
-
@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.
-