I encountered a problem while working on a recent project with adaptive labels:#
This really frustrated me, and it only occurred on iPhone 6 and 6 Plus. At first, I thought it was an issue with my adaptive approach, but after rewriting it several times, I realized that I didn't make any mistakes...
I couldn't figure out what the problem was.
- Today, I saw a code related to adaptive labels posted by a friend in a group chat. I asked for help and found this VerticallyAlignedLabel. I was excited to try it, thinking that I finally found a solution. However, I discovered that while it did align the text vertically, the excess part was still there.
- So, where is the problem? I kept searching and then I found this Multiline UILabel height with Autolayout higher in landscape on Stack Overflow. I had been searching in Chinese before, but couldn't find anything. Finally, when I switched to English, I found similar issues. I thought to myself, surely I'm not the only one facing this problem (how foolish 😢). I also came across this article on objc.io Intrinsic Content Size of Multi-Line Text, and I decided to modify my approach based on their methods.
- Damn it, I couldn't directly use their methods because I had extracted the view, and it was a label on a table view cell. Then, I had a clever idea. It must be because of the width issue. I couldn't make it automatically adjust with the super view's frame, and I couldn't directly calculate the correct width to place it here. I truly admired myself. And then I ran the code...
- Damn it, it still didn't work. Damn it, can't I have some fun? I don't want to live anymore.
-
Just at that moment, I suddenly realized that below where I assigned the value to the label, I had added an extra line of code to increase the spacing between labels. I commented it out and tried again... Hmm, it worked. Don't get too excited, let's try it on the iPhone 6 Plus. And the result... it actually worked. At that moment, I felt like this:
-
There was just one more thing left. After making so many changes, what exactly made it work? Did everything work together? So, I deleted the custom label from the first step, and it didn't cause any issues. Then, I removed the code for calculating the frame, damn it, it didn't work. This line was the key, and that was the end. I spent an entire afternoon doing just two things: a. added one line of code; b. removed one line of code.
This was the initial code:#
And this is the final code:#
Conclusion#
Actually, I should have figured out the reason earlier because this issue only occurred on iPhone 6 and 6 Plus, and the condition in this code specifically targeted devices with a wider screen than the iPhone 5s. But I didn't think about it. Ps (If you encounter the issue of extra height when calculating the label's height, you can try the method provided in step 2 from the link; if the height is too short, check if you missed adding 0.1)
Also, I did miss one line of code, which was to set the width of the label. This happened because I added the view to the controller in viewDidLoad, and at that time, the auto layout hadn't finished yet. (Ps: Normally, setting the frame should be done in viewDidLayoutSubviews) But I usually create it in viewDidLoad and directly assign the frame, so I finally faced the consequences.