B4i Library [class] RichString

Discussion in 'iOS Libraries' started by Erel, Dec 24, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    The RichString class uses NativeObject to create attributed strings. Similar to B4A RichString library.

    For example the text above is created with the following code:
    Code:
    Private Sub Page1_Resize(Width As Int, Height As Int)
       
    Dim rs As RichString
       rs.Initialize(Label1.Text)
       rs.Color(
    Colors.Red, 04).Color(Colors.Blue, 48).SetFont(Font.CreateNewBold(30), 15)
       rs.Strikethrough(
    True510)
       rs.Underline(
    TrueColors.Blue, 010)
       rs.SetFont(
    Font.CreateNewItalic(20), 0, rs.Text.Length-5)
       rs.SetFont(
    Font.CreateNewBold(30), 47)
       rs.BackgroundColor(
    0xFFC0C000520)
       rs.SetToLabel(Label1)
    End Sub
    The range of each attribute is made of two parameters: start index (inclusive) and end index (exclusive).

    Note that if the label was added with the designer then the font size will be reset whenever AutoScaleAll is applied. In that case you need to set it again in Page_Resize event.
     

    Attached Files:

    luke2012, quimacama and imbault like this.
  2. Mark Turney

    Mark Turney Active Member Licensed User

    Looks cool ... thanks Erel!
     
  3. AscySoft

    AscySoft Active Member Licensed User

    Any chance this could be converted to b4a? I know that is already a lib but I'am more interested in custom class.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    B4A RichString library is pretty similar to this class. I don't plan to rewrite it as a custom class.
     
  5. Hugh Thomas

    Hugh Thomas Member Licensed User

    I need to display superscript text. What's the attribute name for superscript?

    Thanks,

    Hugh
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Add this sub to the class:
    Code:
    Public Sub SuperScript(Start As Int, EndIndex As Int) As RichString
       
    Return AddAttribute("NSSuperScript"True, Start, EndIndex)
    End Sub
     
  7. Hugh Thomas

    Hugh Thomas Member Licensed User

    Thanks.

    A couple more I need to complete porting my app. How do I set Bold and Italics? I punted on NSBold and NSItalic but they didn't work.

    Also, how can I set the font size? I tried using rich.SetFont passing it a font created with fnt.CreateNew(size) but it doesn't seem to have any effect.

    Thanks,

    Hugh
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    1. See the example in the first post.
    2. See the last sentence in the first post.
     
  9. Hugh Thomas

    Hugh Thomas Member Licensed User

    Sorry, should have reread the whole thread. I've got Bold and Italic working now.

    The problem with the text size not working wasn't because it was created with the designer - all my UI is created programatically.

    I eventually tracked down the problem to a call to lbl.AdjustFontSizeToFit, which I did after I'd assigned the rich string to the label with rs.SettoLabel(lbl). I thought this would adjust the size of the formatted text, but it looks like it removes some of the formatting.

    I found if I call lbl.AdjustFontSizeToFit *before* I do the rs.SettoLabel(lbl) then it correctly adjusts the size of the formatted text.

    Hugh
     
  10. Turbo3

    Turbo3 Active Member Licensed User

    Any chance of adding richstringformater type so we can just define tokens then apply to a richstring with rs=rsf.format(rs)
     
  11. Hugh Thomas

    Hugh Thomas Member Licensed User

    I've attached a sample app using a routine I wrote to do token-based rich string formatting. It is by no means a general purpose library or class; I wrote it as an interim measure while porting my Android app to iOS. As such it has a hard coded list of tokens it will accept, but it's a simple matter to add/change the tokens so it may be of use until a proper library or class comes along.

    To add a new token you need to do two things:

    1) Add the token to the list of valid tokens. For example, to add a "<yellow>" token:
    Code:
    Private tokens()     As String = Array As String ("<yellow>""<b>""<i>""<st>""<bt>""<ss>""<ul>""<red>")
    2) Add a Case for this token to the select statement in Sub FormatRichString:
    Code:
    Case "<yellow>"
            rich.Color(
    Colors.Yellow, p1, p2)
    Please note that this example includes Erel's RichString class, but I've updated it to include rich.SuperScript()

    I hope this is of some use.

    Hugh

    NOTE: Attached ZIP updated to V0.2 with change to handle any tag, not just tags starting with "<".
     

    Attached Files:

    Last edited: Jun 6, 2015
  12. fbritop

    fbritop Active Member Licensed User

    Is B4A RichString library source code available, so this class (B4I) can be modified to work with tokens?

    Thanks
    FBP
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that it is available in the forum. Note that CSBuilder is available in both B4A and B4i.
     
  14. agraham

    agraham Expert Licensed User

    RichString B4A Eclipse project attached
     

    Attached Files:

    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice