简体   繁体   中英

Multiple lines in UILabel

func setTitleLabel(text: String) {
    let titleLabel = UILabel()
    self.view.addSubview(titleLabel)

    titleLabel.text = text

    titleLabel.lineBreakMode = .ByWordWrapping
    titleLabel.numberOfLines = 2

    titleLabel.font = UIFont(name: "AvenirNext-Medium", size: 22)
    titleLabel.textColor = UIColor(red: 162/255, green: 28/255, blue: 194/255, alpha: 1)

    titleLabel.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.view)
        make.top.equalTo(self.view.snp_top).offset(20)
    }
} 

I have such kind of function for my label, but actually my label text for label is long. What I can do? I wrote this:

titleLabel.lineBreakMode = .ByWordWrapping
titleLabel.numberOfLines = 2

but this didn't help me

Try to also constraint the width of the label. If you don't, it will use its intrinsic content size and the label will become wider so the text can fit in it.

you can use this class to stretch it's font to current label size Class

All you need to do is to tell, that you label is of this class. After that your labels font will automatically be updated to fit into it's rect.

As well there is some IBInspectable properties that can help to setup behavior .

您应该尝试使用numberOfLines如下

titleLabel.numberOfLines = 0

Try to this

@IBOutlet weak var feedNoteLabel: UILabel!

and add this code into viewDidLoad()

feedNoteLabel.numberOfLines = 0
feedNoteLabel.sizeToFit()
feedNoteLabel.layoutIfNeeded()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM