UILabel Incorrectly Sized When Text Only Contains Line Break Characters

Originator:bryn.bodayle
Number:rdar://37764376 Date Originated:2/21/18
Status:Open Resolved:
Product:iOS + SDK Product Version:
Classification:UI/Usability Reproducible:Always
 
Area:
UIKit

Summary:
A UILabel with text only consisting of line breaks is taller than text containing line breaks and other characters.

Steps to Reproduce:
1. Download the sample project.
2. Build and run on any iOS simulator
3. Notice the assertion fails because of different sizes.

The code is actually really simple to reproduce:

    let label1 = UILabel(frame: .zero)
    label1.text = "\n"
    let label1Size = label1.systemLayoutSizeFitting(UILayoutFittingCompressedSize)

    let label2 = UILabel(frame: .zero)
    label2.text = "a\n"
    let label2Size = label2.systemLayoutSizeFitting(UILayoutFittingCompressedSize)

    assert(label1Size == label2Size) // fails

Expected Results:
A UILabel with the text "\n" should have the same height as a UILabel with the text "a\n". 

Actual Results:
A UILabel with the text "\n" has double the height as a UILabel with the text "a\n". The issue is still reproduced even if the label's numberOfLines property is set to 1.

Version/Build:
Xcode Version 9.2 (9C40b)
iOS 11.1 Simulator (Any device type)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!