Create UIViews based on character on JSON string

Does anyone know of a way to implement some code which would automatically create UIViews based on the content of a JSON response?


At the minute, I can retrieve a string and it would show in the UIView as:


This is some text:

• Bullet one

• Bullet two

• Bullet three


I would like, if possible to generate a UIView for every time the sentence starts with "•" and nest it within it's own UIView.


Is that possible?

Which bit are you having problems with:

  • Parsing the string to find the sentences that begin with a bullet?

  • Building views based on that parse result?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I can get the data without issues. I just don't know how I would implement the logic where it would follow and action if it contains (or starts) with the symbol.


I just want to present each sentence beginning with a "•" in it's own UIView so I can style it.

In the action associated to the didEndOnExit event of the textField (you connect the sent event to the action in IB)


Here, you test for prefix of the string, to see if theSentence starts with the char.


var theSentence : String // You have loaded with JSON result
if theSentence.prefix(1) == "•" {
     // set attributes for the text
     // Set the UIView with theSentence
}

I’m still very confused.

Earlier you wrote:

I would like, if possible to generate a UIView for every time the sentence starts with "•" and nest it within it's own UIView.

Do you care about sentences? Or about lines? Sentences are a tricky linguistic concept and quite hard to parse out, so I’m going to assume you’re looking for lines. Let me know otherwise.

Today you wrote:

I just don't know how I would implement the logic where it would follow and action if it contains (or starts) with the symbol.

I’m having a hard time parsing that sentence. I’m going to presume that “follow and action” was meant to be “follow an action”.

Let’s try and make this more concrete, shall we? Here’s some code that walks the lines in a string looking for lines with a specific prefix:

let testString = """
    This is some text:
    • Bullet one
    • Bullet two
    • Bullet three
    """
testString.enumerateLines { (line, _) in
    if line.hasPrefix("• ") {
        print("+", line)
    } else {
        print("-", line)
    }
}

Is that what you’re looking for? If not, please provide more details.

I just want to present each sentence beginning with a "•" in it's own

UIView
so I can style it.

A

UIView
by itself doesn’t display anything, so presumably you’re using a specific view to display this text. What sort of view is that? Our built-in text displaying views (
UILabel
and
UITextView
) handle attributed strings, so you don’t need multiple views to do styling. For example, if you want to style each bulleted line separately, you’d apply the style to an attributed string and then tell a
UITextView
to display that string.

Or do you want to use a custom view for that?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for your reply and this is exactly what I was looking for, I apologise I'm terrible at explaining...


I just use a normal UIView and add a UILabel to it and populate the UILabel with parsed information. Example (with your added code):


        if myVariable?.myString != "" {
            myView.isHidden = true
        } else {
            let anotherVariable = myVariable?.anotherString

            anotherVariable?.enumerateLines { (line, _) in
                if line.hasPrefix("• ") {
                    print("+", line)
    
                } else {
                    print("-", line)
                }
            }
            myLbl.text = anotherVariable
            myLbl.numberOfLines = 0
            myLbl.sizeToFit()
        }


So using your help, it prints how I'd like it to, now, I would just like to display each line with a "•" in it's own UIView in a UILabel. The reason for that is because I will be adding some additional logic at some point where the user clicks the UIView and it turns a different colour.


To give it some context, like this: (link...) ibb.co/hhid4n


I hope that makes sense, apologies again for the confusion.

So if I was to now generate a UIView with a UILabel within it for everytime there is a:


line.hasPrefix("• ")

How would I implement that? Would I need to implement a forEach statement or a for-in loop?


And then the UIView with UILabel would include each time a line begins with "• " as in the screen shot above.


I feel like I have the basic understanding, I'm just uncertain how to put it in to practice.

In the following :


            anotherVariable?.enumerateLines { (line, _) in 
                if line.hasPrefix("• ") { 
                    print("+", line) 
     
                } else { 
                    print("-", line) 
                } 
            }


replace line 3 by the statement to create and populate the UILabel

If you are sure you want to create a UILabel each time ? Why not use a tableView or a collectionView?

I suppose more specifically, I was hoping for some direction on what code I need to write.


I considered a tableView or collectionView, however, because each line is going to be different in length, I do not want to implement an internal scroll on the view and the only way I can prevent that is by defining a height of each cell. But by doing that, it will truncate text that goes beyond the static height properties of the cell.

Code would be something like :


let myLabel = UILabel(frame: aFrame)     // Set the frame where you want the label in the view
myLabel.text = line


You can have cells with varialble height if you want in a tableView. Have a look here for details:

h ttps://www.raywenderlich.com/129059/self-sizing-table-view-cells

Thanks for your help, but not even create UILabels works now.


Think I'm going to give up!

Can you be more specific: what does not work ?


Does it compile ?

What do you get at run ?

Can you show the complete code ?

Create UIViews based on character on JSON string
 
 
Q