2011-11-24 76 views
4

我使用CustomCell而不是在UITableView如何控制UILabels的行間距?

我在CustomCell上放了兩個UILables

Refered這個網站:here,但我失敗了......

問:我如何控制UILabels的行間距?

+0

可能重複[如何提高多標籤兩條線之間的空間?(http://stackoverflow.com/questions/3880526/how-to-increase-a-space-between-lines-in-multiline-label) –

+0

如果你遇到MTLabel的問題,你可以編輯你的問題來描述你做了什麼以及發生了什麼,喜歡。 –

+0

好的!我會再嘗試!謝謝! – hyekyung

回答

13

iOS 6開始,您可以將歸屬字符串設置爲UILabel。檢查以下內容:

NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init]; 
[paragrahStyle setLineSpacing:40]; 
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [labelText length])]; 

cell.label.attributedText = attributedString; 
+0

應該是更清晰的反正thnQ 1+ – ashokdy

+0

@Enkidu請你給我一個解決方案的Swift – nivritgupta

0

編程:

SWIFT 4

使用標籤擴展

extension UILabel { 

    // Pass value for any one of both parameters and see result 
    func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) { 

     guard let labelText = self.text else { return } 

     let paragraphStyle = NSMutableParagraphStyle() 
     paragraphStyle.lineSpacing = lineSpacing 
     paragraphStyle.lineHeightMultiple = lineHeightMultiple 

     let attributedString:NSMutableAttributedString 
     if let labelattributedText = self.attributedText { 
      attributedString = NSMutableAttributedString(attributedString: labelattributedText) 
     } else { 
      attributedString = NSMutableAttributedString(string: labelText) 
     } 

     // Line spacing attribute 
     attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) 

     self.attributedText = attributedString 
    } 
} 

現在調用擴展功能

let label = UILabel() 
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" 

// Pass value for any one argument - lineSpacing or lineHeightMultiple 
label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0 

// or try lineHeightMultiple 
//label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0 


或者使用標籤實例(只複製&執行該代碼來查看結果)

let label = UILabel() 
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" 
let attrString = NSMutableAttributedString(string: stringValue) 
var style = NSMutableParagraphStyle() 
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 

// Line spacing attribute 
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) 

// Character spacing attribute 
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length)) 

label.attributedText = attrString 

斯威夫特3
只需複製&執行該代碼,看看結果。

let label = UILabel() 
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" 
let attrString = NSMutableAttributedString(string: stringValue) 
var style = NSMutableParagraphStyle() 
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) 
label.attributedText = attrString 

從界面生成器:

enter image description here