Swift 标签中的换行符和行数(以编程方式)
Line Breaks and Number of Lines in Swift Label (Programmatically)
通过 select 在情节提要中添加标签,我可以 select Line Break
变为 Word Wrap
并将行数更改为大于 1。如何才能我在 Swift?
中以编程方式执行此操作
您可以执行此操作以编程方式设置它
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.numberOfLines = 3
Swift 3/4
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 3
如果您希望标签有多行,请执行以下操作:
var myLabel:UILabel = UILabel(frame: CGRectMake(7, 200, 370, 100))
myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
myLabel.numberOfLines = 0 //'0' means infinite number of lines
Do remember to increase the height in "CGRectMake(7, 200, 370, 100)" <-- This
Otherwise the label won't be able to take the multiple lines of text.
注意 Swift 3 您需要使用更新的方法 byWordWrapping
productNameLabel.lineBreakMode = .byWordWrapping
productNameLabel.numberOfLines = 1
或者在末尾添加 Ellipsis 使用 byTruncatingTail
productNameLabel.lineBreakMode = .byTruncatingTail
productNameLabel.numberOfLines = 1
通过 select 在情节提要中添加标签,我可以 select Line Break
变为 Word Wrap
并将行数更改为大于 1。如何才能我在 Swift?
您可以执行此操作以编程方式设置它
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.numberOfLines = 3
Swift 3/4
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 3
如果您希望标签有多行,请执行以下操作:
var myLabel:UILabel = UILabel(frame: CGRectMake(7, 200, 370, 100))
myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
myLabel.numberOfLines = 0 //'0' means infinite number of lines
Do remember to increase the height in "CGRectMake(7, 200, 370, 100)" <-- This
Otherwise the label won't be able to take the multiple lines of text.
注意 Swift 3 您需要使用更新的方法 byWordWrapping
productNameLabel.lineBreakMode = .byWordWrapping
productNameLabel.numberOfLines = 1
或者在末尾添加 Ellipsis 使用 byTruncatingTail
productNameLabel.lineBreakMode = .byTruncatingTail
productNameLabel.numberOfLines = 1