ios - UILabel sizeToFit 拆分数字

标签 ios swift uilabel

我有一个 UILabel,它在垂直线上显示一组数字;但是,它将两位或更多位数字分成不同的行。我该如何解决这个问题?

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()
}

Here is a screenshot of what this code produces

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

设置标签的宽高和行数

  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!)

输出是

   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

关于ios - UILabel sizeToFit 拆分数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41734501/

相关文章:

iphone - 如何用 viewWithTag 隐藏 UILabel?

ios - 带有 unicode 的字符串和带有多行的 UILabel

ios - Objective-C . 追踪无法识别的选择器

ios - Apple Accelerate vDSP fft vs DFT 和缩放因子

ios - 从中心变换 UIView

ios - 分配文本时 UITextView 不滚动

ios - MSMessagesAppViewController 没有正确调用 didSelect() ?

ios - 在 Swift 框架中使用 Realm 为同样使用 Realm 的主机 iOS 应用程序带来挑战

ios - Facebook 登录 - Firebase V3

ios - 通过 CAGradientLayer 在 IOS 的 UIImageView 中添加渐变层