var testLabel = UILabel(frame: CGRectMake(8,0,tableView.frame.width-8,100))
let testDesc = "dsfdddfdsfdsfsdfdsfdsfdsfdsfdsfsdfdsfsdfsdfdsfdsfdsfdsfdsfdsfdsfdsfdsfdsfdsfdsfdsf"
var labelString = indexString + ". " + testDesc
testLabel.text = labelString
testLabel.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
testLabel.numberOfLines = 0
retCell.addSubview(testLabel)
我的输出:
1. ksajdkasdsajdksajdksajd
asjdkjassadkasldkalsdklsakdl
如何使我的输出如下:
1. asdasdasdasdasdasdasds
djaskdjsadjksadasjdjas
答案 0 :(得分:3)
以下是我使用的代码
let paragraph = NSMutableParagraphStyle()
paragraph.firstLineHeadIndent = 0
paragraph.headIndent = 8 //You can change this to whatever indent you want
let mutString = NSAttributedString(
string: "this is a test but I need this to be long so it flows onto multiple lines so I will keep going",
attributes: [NSAttributedStringKey.paragraphStyle: paragraph]
)
mainLabel.attributedText = mutString