Forum Archive

Just for fun: ui.ButtonItem multi-lines

cvp
import ui
from objc_util import *

v = ui.View()

def b_action(sender):
    print(sender)

b = ui.ButtonItem()
b.action = b_action

# set a ButtonItem custom view as an ui.Button where we can, via ObjectiveC
# put its title (label text) as multilines
vv = ui.Button()
vv.title = 'aaa\nbbb'
vv.tint_color = 'green'
vv.action = b_action
vv.frame = (0,0,100,32)
vvo = ObjCInstance(vv)
for sv in vvo.subviews(): 
    if hasattr(sv,'titleLabel'):
        tl = sv.titleLabel()
        tl.numberOfLines = 0
        tl.adjustsFontSizeToFitWidth = True # auto resize font to fit
        break
ObjCInstance(b).customView = vvo

v.right_button_items = (b,)
v.present() 

stephen

this is wonderfull! this should be implemented in the ui module as standard functionality! great job @cvp as usual

mikael

@cvp, thanks for the multiline but also for making me aware of adjustsFontSizeToFitWidth. I am a bit cunfused, though, as the docs say it only applies to single-line labels.

cvp

@mikael you're right, perhaps is adjustsFontSizeToFitWidth not needed... The doc says
"only intended for use with a single-line label". Not very clear.
Some tests show it does not work.
Thanks for the correction, thus let us keep only the multi-line aspect 🙄

stephen

@cvp @mikael possibly documented because Developer may not of done extensive testing with larger numbers of lines? or even testing difernt text wraping? just to cover the posibility of bugs between releases..?