2017-01-19 44 views
1

我有一个UILabel显示一个垂直行中的数字数组;然而,它将两位或更多位的数字分解为单独的行。我该如何解决这个问题?UILabel sizeToFit拆分数字

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

任何帮助将非常感激。

在此先感谢。

+0

增加'UILabel'的宽度,您可以使用''NSString'大小(属性)'方法来计算字符串的计算长度,或者简单地确保你的'UILabel'足够长,以包含任何需要的字符串长度。你还可以设置'UILabel'属性'numberOfLines'为1,这样就可以无字换行,然后将属性'adjustFontSizeToFitWidth'设置为true,字符串将缩小为适合你的标签。 –

+0

使用syllableLabe.numberOfLines = 4。 – rvx

回答

0

设置标签宽度,高度和线

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