iOS Question TableView add centered item??

ilan

Expert
Licensed User
Longtime User
hi

can i add an item to the tableview where the TableCell text is centered?

i create the text with AttributedString.

thanx
 

ilan

Expert
Licensed User
Longtime User
i tried it by setting tc.IndentationLevel = 1 and then i calculate the text width like this:

B4X:
        tc.IndentationLevel = 1
 tc.IndentationWidth = (tablevpnl.Width/2) - (tc.Text.ToString.MeasureWidth(Font.CreateNew(16*density))/2)

the result is not 100% centered but its something :)

anyone got a better solution?
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

use this sub to create the AttributedString :)

B4X:
Sub CreateCenteredAttrString(Text As String, aFont As Font, Color As Int) As AttributedString
    Dim NaObj1 As NativeObject
    NaObj1 = NaObj1.Initialize("NSMutableParagraphStyle").RunMethod("new",Null)
    NaObj1.SetField("alignment",1)
   
    Dim Attr As Map = CreateMap("NSColor":NaObj1.ColorToUIColor(Color),"NSParagraphStyle":NaObj1,"NSFont":aFont)
    Dim NaObj2 As NativeObject
    NaObj2 = NaObj2.Initialize("NSAttributedString").RunMethod("alloc",Null).RunMethod("initWithString:attributes:",Array(Text,Attr.ToDictionary))
   
    Return NaObj2
End Sub

Jan
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi,

use this sub to create the AttributedString :)

B4X:
Sub CreateCenteredAttrString(Text As String, aFont As Font, Color As Int) As AttributedString
    Dim NaObj1 As NativeObject
    NaObj1 = NaObj1.Initialize("NSMutableParagraphStyle").RunMethod("new",Null)
    NaObj1.SetField("alignment",1)
 
    Dim Attr As Map = CreateMap("NSColor":NaObj1.ColorToUIColor(Color),"NSParagraphStyle":NaObj1,"NSFont":aFont)
    Dim NaObj2 As NativeObject
    NaObj2 = NaObj2.Initialize("NSAttributedString").RunMethod("alloc",Null).RunMethod("initWithString:attributes:",Array(Text,Attr.ToDictionary))
 
    Return NaObj2
End Sub

Jan


sorry i dont understand

i create the attributedString in b4i with iSTringUtils so no problem with that. i only need to center my text. is it possible with the sub you posted?

this is how i create my TableCell

B4X:
    For i = 0 To Main.sal_timelist.Size - 1
        Dim timeproc As saltotalcalc =  Main.sal_timelist.Get(i)
        Dim str As AttributedString
        str.Initialize(timeproc.procent & "%" & "     -->     " & Floor(timeproc.minutes/60) & ":" & NumberFormat2(timeproc.minutes Mod 60,2,0,0,False), Font.CreateNew(16*density),Colors.RGB(43,43,43))
        Dim tc As TableCell = timeTableview.AddSingleLine("")
        tc.Text = str
        tc.AccessoryType = tc.ACCESSORY_DETAIL_BUTTON
        tc.IndentationLevel = 1
        tc.IndentationWidth = (tablevpnl.Width/2.5) - (tc.Text.ToString.MeasureWidth( Font.CreateNew(16*density))/2)
    Next
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
is it possible with the sub you posted?
Yes.

Do it like this:

B4X:
For i = 0 To Main.sal_timelist.Size - 1
        Dim timeproc As saltotalcalc =  Main.sal_timelist.Get(i)
        Dim tc As TableCell = timeTableview.AddSingleLine("")
        tc.Text = CreateCenteredAttrString(timeproc.procent & "%" & "     -->     " & Floor(timeproc.minutes/60) & ":" & NumberFormat2(timeproc.minutes Mod 60,2,0,0,False), Font.CreateNew(16*density),Colors.RGB(43,43,43))
        tc.AccessoryType = tc.ACCESSORY_DETAIL_BUTTON
Next

Jan
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
thanx a lot @JanPRO i am not getting a 100% centered result but its good enough for me :)

screenshot1.png
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
If you are not 100% happy you could also try to create a custom cell with the CustomView property of TableCell.

Jan

thanx but i dont know how to do that and i am very happy with the result so i will leave it like this for now.
i was thinking of using a CLV instead of tableview, there it is much simpler to set the gravity since you use a simple Label.
but tableview is faster to setup so i will use for now the tableview as is.

thanx a lot Jan :)
 
Upvote 0
Top