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 Nice, as usual...
-
@mikael very nice!
-
@mikael you could add an outline tag
# red sub-part UIColor=ObjCClass('UIColor').redColor() mystr.addAttribute_value_range_(ns('NSColor'),UIColor,NSRange(st,ll)) outlineWidth = -3 # width in percentage of font size # >0 => stroke only # <0 => stroke and fill color outlineColor = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(0,0,0,1) mystr.addAttribute_value_range_(ns('NSStrokeColor'), outlineColor, NSRange(st,ll)) mystr.addAttribute_value_range_(ns('NSStrokeWidth'), outlineWidth, NSRange(st,ll))
-
@cvp, fancy!
Ok, so now there is an
o
oroutline
tag that you can use as-is or with an optional color and/or width. Results with this string:r.set_rich_text("\n".join([ "Plain", "<f Zapfino><c red>Color</c></f>", "<b>Bold <i>italic</i></b>", "and <i><f system 32>just</f> italic</i>", "Outlines:", "<o>DEFAULT</o>", "<o 3>THICK</o>", "<o 3 blue>COLORED</o>", "<o -2><c orange>FILLED</c></o>", ]))
... look like this:
-
@mikael sincerely, you're a champion
-
@cvp, now that you got me started, how could we say anything important without underlining it? (Strikethrough thrown in because it was just 4 extra lines of code.)
Underline tag is
u
orunderline
, strikethrough iss
orstrike
. Both take a suitable combination of style qualifiers:thick
,double
,dot
,dash
,dashdot
,dashdotdot
,byword
.So now with this:
"Plain", "<f Zapfino><c red>Color</c></f>", "<b>Bold <i>italic</i></b>", "and <i><f system 32>just</f> italic</i>\n", "<u>Outlines:</u>\n", "<o>DEFAULT</o>", "<o 3>THICK</o>", "<o 3 blue>COLORED</o>", "<o -2><c orange>FILLED</c></o>\n", "<s double byword>really not cool</s>"
... we get this:
-
@mikael one more time, whaaaa
-
@mikael and a last one for today
UIColor=ObjCClass('UIColor').redColor() mystr.addAttribute_value_range_(ns('NSColor'),UIColor,NSRange(st,ll)) shadow = ObjCClass('NSShadow').alloc().init() shadow.setShadowOffset_(CGSize(2,2)) shadowColor = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(0,0,0,1) shadow.setShadowColor_(shadowColor) shadow.setShadowBlurRadius_(3) mystr.addAttribute_value_range_(ns('NSShadow'), shadow, NSRange(st,ll))
-
@cvp, you ruthless taskmaster!
So, no more
s
tag, you now need to use the longerstrike
orshadow
.shadow
can be customized with color (default'grey'
), offset (default(2,2)
) and blur (default3
).Now with:
"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>", "<u>Outlines:</u>", "<b>", "<o>DEFAULT</o>", "<o blue>COLORED</o>", "<o -3><c orange>FILLED</c></o>", "</b>", "<strike double byword>really not cool</s>"
We get:
-
-
@mikael Finally, it is perhaps better to define the text in html and use standard functions
import ui from objc_util import * # main view v = ui.View() v.frame = (0,0,300,200) v.background_color = 'white' v.name = 'ui.Label with html' # label l = ui.Label() l.frame = (10,0,200,200) l.number_of_lines = 0 v.add_subview(l) html = """ <html> <head> <title>Title of the document</title> <style> .shadow { text-shadow: 2px 2px #1c87c9; } </style> </head> <body> <h2>Text-shadow property example</h2> <p>Some paragraph for example.</p> <p class="shadow">Some paragraph with the text-shadow property.</p> </body> </html> """ data = ns(html.encode()) mystr = ObjCClass('NSMutableAttributedString').alloc().initWithHTML_documentAttributes_(data,None) lobj = ObjCInstance(l._objc_ptr) lobj.setAttributedText_(mystr) v.present('sheet')
-
@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.)