iOS Code Snippet A good thing for you MarkPatternCSbuilder

Discussion in 'iOS Code Snippets' started by Pooya1, Aug 17, 2018.

  1. Pooya1

    Pooya1 Active Member Licensed User

    I could detect link or any thing with Regex in android and use it
    But in B4i i had problem about convert my custom pattern to link
    Now with below code you can convert any string with pattern to link
    Sub MarkPatternCSbuilder(Input As String, Pattern As String, GroupNumber As Int) As CSBuilder
    Dim cs As CSBuilder
    Dim lastMatchEnd As Int = 0
    Dim m As Matcher = Regex.Matcher(Pattern, Input)
    Do While m.Find
    Dim currentStart As Int = m.GetStart(GroupNumber)
           cs.Append(Input.SubString2(lastMatchEnd, currentStart))
           lastMatchEnd = m.GetEnd(GroupNumber)
    'apply styling here
    'number should match number of stylings set.
    If lastMatchEnd < Input.Length Then cs.Append(Input.SubString(lastMatchEnd))
    Return cs
    End Sub
    Example for detect hashtag:
    Label1.AttributedText = MarkPatternCSbuilder(Label1.Text,"\B(#\w+)\b",1)
    Alexander Stolte and Erel like this.
  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