iOS Question [SOLVED] Can i use a String format on a label on B4i?

Carlos Huerta

Member
Licensed User
Greetings to everyone!



I'm Carlos and this is my first message here. First of all, i want to thank everyone on this community, i've learned a lot because of your examples and questions. Thanks so much!.

I usually make apps on B4A but recently, i've acquired a license for B4i. It's works like a charm!

But i got a question. On B4A i usually use this to format a label text:

B4X:
 l.Text = format("| %1$-5.10s | %2$11.30s | %3$12.30s | %4$10.30s | %5$10.20s",Array(Value1,Value2,Value3,Value4,Value5))
Where "l" is a Label and Value1 to 5 are some random values (Some strings and some integers). I'm using this (taken from an example on the B4A forum) to format the text before using it on the label:

B4X:
Sub inline As JavaObject
    Return Me
End Sub

Sub format(f As String,a() As Object) As String
    Return     inline.RunMethod("format",Array(f,a))
End Sub
#if java
import java.lang.String;

static public String format(String f,Object... args){
    return String.format(f,args);
}
#end if
I want to do the same, but on B4i!. Is there any chance for you to help me with this? Is it possible to do something similar on B4i?.

In any case, i want to thank you again for all your efforts. This community is amazing!.

Regards from Chile and have an awesome week!!!.


- Carlos
 

Carlos Huerta

Member
Licensed User
Check the smart strings literal: https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/#content
It supports number formatting.

Erel! Thanks so much for reply, and thanks so much for your work here!. A lot of my apps were made because of your help, so let me give you a pizza someday haha!.

I use the number formatting and works really great! Just what i want it. But i got another question: Is there anyway to specify an certain number of blank spaces for each variable?.

For example (Java Code):

B4X:
String.format("|%20d|", 93);   ' prints: |                  93|
This is what i want to achieve!. Is there any chances of recreate this on B4i?

Thanks again and have a great day! :)


- Carlos
 

Carlos Huerta

Member
Licensed User
Erel, you are amazing!!!. Thanks so much ^^!

Your way to get rid of the problem is so good, thanks so much for that!. I tried something similar using Objective C code, but i like the way you handle it. Here is my code if anyone find it useful (But the Erel's way is much better than mine):

B4X:
'Taken from an Erel's example on the forum
Dim NativeMe As NativeObject = Me
result1 = NativeMe.RunMethod("padding:spaces:", Array("EXAMPLE",20)).AsString


Dim s1 as String
s1 = " |"&result1&"|"

Label1.Font = Font.CreateNew2("Inconsolata",20)    'I use the "Inconsolata" font because of his monospace property
Label1.Text = s1


' I didn't know how to handle the padding, so i tried something similar on Objective C with an example on the internet (All the credits are for them)

#If OBJC                          

- (NSString *) padding: (NSString *) on spaces:(int)spaces{
    NSString *str = on;
    int add = spaces-[str length];
    if (add > 0) {
        NSString *pad = [[NSString string] stringByPaddingToLength:add withString:@" " startingAtIndex:0];
        str = [pad stringByAppendingString:str];
    }
    return str;
}

#End If

Thanks so much and have a great week!!!


- Carlos
 
Top