Forum Archive

how to set the font size of segmented control titles

frankL

how can I set the font size of segmented control titles? The font size it chooses is very small regardless of the number of tabs.
I tried including a font definition in the ui.SementedControl like this:
my_sc = ui.SementedControl(font=('Arial Rounded MT Bold',22))

but regardless of the selection of the font size (vs 22) the displayed tab title size does not change.

cvp

@frankL try this

import ui
from objc_util import *

v = ui.SegmentedControl()
v.background_color = 'white'
v.frame =(0,0,300,50)
v.segments = ['Seg1','Seg2','Seg3','Seg4']

UIFont = ObjCClass('UIFont').fontWithName_size_('Arial Rounded MT Bold',22)
attributes = {'NSFont': UIFont}
vo= ObjCInstance(v).segmentedControl()
vo.setTitleTextAttributes_forState_(attributes, 0)

v.present('sheet')
frankL

@cvp said:

UIFont = ObjCClass('UIFont').fontWithName_size_('Arial Rounded MT Bold',22)
attributes = {'NSFont': UIFont}
vo= ObjCInstance(v).segmentedControl()
vo.setTitleTextAttributes_forState_(attributes, 0)

That's perfect, thank you.

cvp

@frankL still better

import ui
from objc_util import *

v = ui.View()
v.frame = (0,0,400,200)
v.background_color = 'white'

sc = ui.SegmentedControl()
items = ['abcd','abcd','abcd','abcd']
d = (v.width-2*10)/len(items)
sc.segments = items
sc.frame = (10,10,d*len(items),d)

o = ObjCInstance(sc).segmentedControl()
for i in range(len(items)):
    with ui.ImageContext(d,d) as ctx:
        #path = ui.Path.rounded_rect(0,0,d,d,5)
        #ui.set_color('lightgray')
        #path.fill()
        s = 14 + i*3
        font = ['Menlo', 'Bradley Hand', 'Courier New', 'Arial Rounded MT Bold'][i]
        color = ['red', 'blue', 'green', 'brown'][i]
        ui.draw_string(items[i], rect=(0,(d-s)/2,d,s), font=(font, s), color=color, alignment=ui.ALIGN_CENTER)
        ui_image = ctx.get_image().with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
    o.setImage_forSegment_(ui_image.objc_instance,i)
v.add_subview(sc)

v.present('sheet')