Search code examples
iosswifttableviewuilabel

show bullets in label on tableview cell swift ios


image is attached for reference

I have a table view, inside the table view I have multiple cells as shows in attached image. I want to show amenities items like : air conditioning, swimming pool, gym, tv cable etc like bullets.

How can I achieve this

I have all these value in my model coming from API response.

          let ac = "Air Conditioning"
          let tv = "TV Cable"
          let washing = "Washing Machine"
          let connectivity = "Wi-Fi"
          let gym = "Gym"

Solution

  • Good case for stack views...

    • Vertical UIStackView
    • Each "row" is a Horizontal UIStackView with Distribution: Fill Equally

    Looks like this - showing Bounds Rectangles:

    enter image description here

    without the Bounds Rectangles:

    enter image description here

    view hierarchy:

    enter image description here


    Edit

    A second approach (which you may find easier)...

    Use a single horizontal stack view with a single label on each "side" and use .attributedText.

    Set the .attributedText of each label to a formatted, bulleted attributed string.

    The cell prototype could look like this:

    enter image description here

    Then, use this code (slightly modified from https://stackoverflow.com/a/46889728/6257435) to generate a formatted, bulleted attributed string:

    func add(bulletList strings: [String],
             font: UIFont,
             indentation: CGFloat = 15,
             lineSpacing: CGFloat = 3,
             paragraphSpacing: CGFloat = 10,
             textColor: UIColor = .black,
             bulletColor: UIColor = .red) -> NSAttributedString {
        
        func createParagraphAttirbute() -> NSParagraphStyle {
            var paragraphStyle: NSMutableParagraphStyle
            let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]
            
            paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
            paragraphStyle.tabStops = [
                NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
            paragraphStyle.defaultTabInterval = indentation
            paragraphStyle.firstLineHeadIndent = 0
            paragraphStyle.lineSpacing = lineSpacing
            paragraphStyle.paragraphSpacing = paragraphSpacing
            paragraphStyle.headIndent = indentation
            return paragraphStyle
        }
        
        let bulletPoint = "\u{2022}"
        let textAttributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: textColor]
        let bulletAttributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: bulletColor]
        let buffer = NSMutableAttributedString.init()
        
        for string in strings {
            var formattedString = "\(bulletPoint)\t\(string)"
    
            // don't add newLine if it's the last bullet
            if string != strings.last {
                formattedString += "\n"
            }
    
            let attributedString = NSMutableAttributedString(string: formattedString)
            let paragraphStyle = createParagraphAttirbute()
            
            attributedString.addAttributes(
                [NSAttributedString.Key.paragraphStyle : paragraphStyle],
                range: NSMakeRange(0, attributedString.length))
            
            attributedString.addAttributes(
                textAttributes,
                range: NSMakeRange(0, attributedString.length))
            
            let string:NSString = NSString(string: formattedString)
            let rangeForBullet:NSRange = string.range(of: bulletPoint)
            attributedString.addAttributes(bulletAttributes, range: rangeForBullet)
            buffer.append(attributedString)
        }
        
        return buffer
    }
    

    A quick implementation of that using your image as sample data results in this:

    enter image description here