0

I have a label which is pretty easy. Like this:

    let label = UILabel()
    label.numberOfLines = 3
    label.text = "hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!"
    label.snp.makeConstraints { (make) in
        make.left.top.equalToSuperview().offset(30)
        make.right.equalToSuperview().offset(30)
    }
    addSubview(label)

I have assigned label.numberOfLines = 3, Now I want to know that the third line's text length. How can I calculate it ? Thanks

Mani
  • 2,599
  • 4
  • 30
  • 49
Longshihua
  • 395
  • 1
  • 3
  • 20
  • https://stackoverflow.com/a/7174080/2700586 Duplicate of question – Mani Jul 22 '17 at 10:11
  • @Mani,Thanks. But I don't mean that. I known how to get a dynamic height for UILabel. Actually, I want to know how many characters does the third line contains? Or how to get the text length for every line . I used the snapkit to layout the label, I don't need to calculate the height. – Longshihua Jul 22 '17 at 14:19
  • Do UILabels actually render out selectable text? My assumption has always been that it basically outputs an image. In any case, I would play around with a subclass of UILabel and see if you can get any info during its draw phase. – solenoid Jul 22 '17 at 17:34
  • @JackLong, Refer this https://stackoverflow.com/a/14413484/2700586 From this array position indicates the ordinal of line and then you can calculate the length of that string in that position – Mani Jul 23 '17 at 00:22
  • @Mani, thanks, it can help me. – Longshihua Jul 23 '17 at 07:44

0 Answers0