iOS Question Add CRLF of SegmentedControl item

JanPRO

Well-Known Member
Licensed User
I didn't find a simpler way:

B4X:
Dim Items As List
    Items.Initialize

    Items.Add("One" & CRLF & "_1_")
    Items.Add("Two" & CRLF & "_2_")

    SetItems(SegmentedControl1,Items,Font.CreateNew(25),False)


Sub SetItems(SC As SegmentedControl,Items As List,aFont As Font, Animated As Boolean)

    Dim NaObj2 As NativeObject = SC
    NaObj2.RunMethod("removeAllSegments", Null)

    For i = 0 To Items.Size -1
        Dim lbl As Label
        lbl.Initialize("")
    
        Dim NaObj As NativeObject = lbl
        NaObj.SetField("numberOfLines",0)
    
        lbl.Text = Items.Get(i)
        lbl.TextAlignment = lbl.ALIGNMENT_CENTER
        lbl.Height = SC.Height
        lbl.Width = SC.Width / Items.Size
    
        Dim cv As Canvas
        cv.Initialize(lbl)
        NaObj2.RunMethod("insertSegmentWithImage:atIndex:animated:",Array(cv.CreateBitmap,i,Animated))
    Next

End Sub

But it works :)
 
Last edited:

Erel

Administrator
Staff member
Licensed User
This is a partial solution as it will only work properly if the first line is longer than the second:
B4X:
   Dim no As NativeObject = Me
   no.RunMethod("setMultipleLines", Null)

#if objc
- (void)setMultipleLines {
   [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
}
#end if
Now all SegmentedControls will support multiline items:
B4X:
SegmentedControl1.SetItems(Array("first line" & CRLF & "second", "first line" & CRLF & "second"))
 
Top