Home > Blockchain >  How to show pyramid pattern on UILabel on Button Click(swift language)
How to show pyramid pattern on UILabel on Button Click(swift language)

Time:08-03

///swift iOS /// I have tried with below code

@IBOutlet weak var myLbl: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()
    
    
}
func magic(choose: String, row: Int) -> String {
    var name = ""
    
    for i in 1...row {
        
        name  =  (String.init(repeating: " ", count: row-i) String.init(repeating: choose, count: 2*i-1))
        
        
    }
    
    return name
}

@IBAction func btnPressed(_ sender: UIButton) {
    
    let result  = magic(choose: "0", row: 5)
    myLbl.text  = result
}

plz check images which type of response I am getting }[I am getting output at this manner][1] [1]: https://i.stack.imgur.com/YdV8f.png

we have to show this type of response on my UILabel

CodePudding user response:

Please open up a fresh playground and paste the following code in:

import UIKit

let label = UILabel()

label.text = " 0 \n000"      // simple demo text
label.numberOfLines = 0      // unlimited number of lines
label.font = .monospacedSystemFont(ofSize: 14, weight: .regular) // monospaced font so that columns are aligned
label.sizeToFit()            // tell the label to evaluate its content and adjust its size to fit

It should display a basic example of what you want and give you all the information you need to update your code accordingly.

  • Related