2017-01-19 45 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