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

    Hi
    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
    Code:
    Sub MarkPatternCSbuilder(Input As String, Pattern As String, GroupNumber As Int) As CSBuilder
       
    Dim cs As CSBuilder
       cs.Initialize
       
    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
           cs.Color(0xFF03FFFF)
           cs.Link(m.Group(GroupNumber))
           cs.Append(m.Group(GroupNumber))
           cs.Pop.Pop 
    'number should match number of stylings set.
       Loop
       
    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)
     
    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