How to find out the width of the last line of the label?

In my application, I have a label whose text came from the server, so I don’t know its width, and at the end of this label should be UIImage.

My problem: I do not know how to position the image due to the non-static width of the label text.

To be more clear, this is a snapshot of the form and how it should be:

snapshot

Any suggestion to solve this problem please?

+1
source share
4 answers
  • Set the text to UILabel
  • Get the width of this UILabel using yourUILabel .frame.width
  • Set the x coordinate of your UIImage to yourUILabel.frame.width + emptySpace as shown

     var yourUIImageView:UIImageView = UIImageView(frame: CGRectMake(x:PaddingFromLeft + yourUILabel.frame.width + emptySpace, y: yourYCoordinate, width: yourImageWidth, height : yourImageHeight)) 
0
source

You may be able to insert your image directly on the label by doing this

 var attachment = NSTextAttachment() attachment.image = UIImage(named: "your_image_name") var attributedString = NSAttributedString(attachment: attachment) var labelString= NSMutableAttributedString(string: "Lorem ipsum dolor sit ame...") labelString.appendAttributedString(attributedString) yourUILabel.attributedText = labelString 
+3
source

I managed to solve this problem. This is not the most beautiful code, but it works. I return the word number from the last line of the label, from which I can calculate the width from which the text ends, and the image begins to count (x, y).

 func lastWordInTitle(title: String) -> Int { var separateWords: [String] = [] var sizeOfWords: [CGFloat] = [] var wordsRemaining: Int = 0 var wordsWidthInOneLine: CGFloat = 0 let font = titleLabel.font let fontAttr = [NSAttributedStringKey.font: font] title.enumerateSubstrings(in: title.startIndex..<title.endIndex, options: .byWords) { (substring, _, _, _) in if let substring = substring { separateWords.append(substring) // number of words in label sizeOfWords.append(substring.size(withAttributes: fontAttr).width + 8) //size of each word + 8 for the space between them } } wordsRemaining = separateWords.count print("SSS wordsRemaining initial \(wordsRemaining)") var wordsToRemoveIndex = 0 for index in 0..<separateWords.count { wordsWidthInOneLine += sizeOfWords[index] wordsToRemoveIndex += 1 if wordsWidthInOneLine > titleLabel.frame.width { if index == separateWords.count - 1 { wordsRemaining -= wordsToRemoveIndex return 1 } else { wordsRemaining -= wordsToRemoveIndex - 1 == 0 ? 1 : wordsToRemoveIndex - 1 wordsToRemoveIndex = 0 wordsWidthInOneLine = 0 wordsWidthInOneLine = sizeOfWords[index] } } else if wordsWidthInOneLine < titleLabel.frame.width && index == separateWords.count - 1 { let reversedSeparateWordsSize = Array(sizeOfWords.reversed()) var width: CGFloat = 0 for index in 0..<wordsRemaining { width += reversedSeparateWordsSize[index] } if width > titleLabel.frame.width { return wordsRemaining - 1 } return wordsRemaining } } return wordsRemaining } 
+1
source

It actually depends ...

You might want to use the sizeToFit or sizeThatFits .

0
source

Source: https://habr.com/ru/post/918937/


All Articles