0

I tried a many solutions but not get a right answer.

Here my code:

self.AhadeesView = UILabel()
self.AhadeesView.translatesAutoresizingMaskIntoConstraints = false
self.AhadeesView.backgroundColor = UIColor.orange
self.AhadeesView.numberOfLines = 0
self.AhadeesView.text = NSLocalizedString("TitleAhadees", comment: "Title Ahadees of the App")
self.AhadeesView.textAlignment = .center
self.AhadeesView.font = UIFont(name:"Jameel-Noori-Nastaleeq",size:25)
self.AhadeesView.lineBreakMode = NSLineBreakMode.byWordWrapping
//   self.AhadeesView.sizeToFit()

containerView1.addSubview(AhadeesView)

1 Answers1

-1

Seems like you have missed your frame to show the label.

self.AhadeesView = UILabel(frame: [your frame value])

Frame can be created by

let frame = CGRect(x: 0, y: 0, width: 320, height: [height_of_String])

[height_of_String] can be calculated by below extension

You can calculate the width and height of the string using the following extension methods

extension String {
    //To calculate the height u need to pass the width of the label and the required font size.

    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
        return ceil(boundingBox.height)
    }

    func width(withConstraintedHeight height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
        return ceil(boundingBox.width)
     }
}
Prakash
  • 90
  • 1
  • 8