简体   繁体   中英

UILabel sizeToFit breaking apart numbers

I have a UILabel that is displaying an array of numbers in a vertical line; however, it is breaking apart numbers that are two or more digits into separate lines. How can I solve this?

syllableArr = ["6", "5", "5", "11"]

func updateSyllableLabel() {

    //Updates content of syllable label

    let multiLineString = self.syllableArr.joined(separator: "\n")
    syllableLabel.text = multiLineString
    syllableLabel.numberOfLines = 0
    syllableLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
    syllableLabel.sizeToFit()
}

以下是此代码生成的屏幕截图

Any help would be much appreciated.

Thanks in advance.

Set label width,height and set number of lines

  let syllableArr = ["6", "5", "5", "11"]
  let multiLineString = syllableArr.joined(separator: "\n")
  let syllableLabe = UILabel(frame: CGRect(x: 10, y: 20, width: 10, height: 80)) x,y width and height set according you

  syllableLabe.text = multiLineString
  syllableLabe.numberOfLines = 4
  syllableLabe.lineBreakMode = NSLineBreakMode.byWordWrapping
  print(“label text ===%@",syllableLabe.text!)

Out Put is

   label text ===%@6
                  5
                  5
                  11

 if you want to break 11 
 let str: String = syllableArr[3]
 str = ""11"
 let digits = String(str).characters.map { Int(String($0))! }
 print("digits is ---%@",digits) and create new array as this
 let newsyllableArr = ["6", "5", "5", "1","1"] and use same logic above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM