I just cut the original string \\nfoo\\n\\nbar\\n\\nfoo\\n\\nbar\\n
into two different strings \\nfoo\\n\\nbar\\n
and \\nfoo\\n\\nbar\\n
. The cut strings will be set into its dedicated label. I was expecting that the combined height of the two labels would just be the same with a label that has the original string. Can you guide me into something that explains this? Is there a way to make the combined two labels the same with the one label without adding/deleting a character from the cut strings?
Playground
import PlaygroundSupport
import Foundation
import UIKit
PlaygroundPage.current.needsIndefiniteExecution = true
let viewWidth: CGFloat = 100.0
func createLabel(_ text: String, _ backgroundColor: UIColor = .white) -> UILabel {
let label = UILabel()
label.text = text
label.numberOfLines = 0
label.backgroundColor = backgroundColor
label.textColor = .black
label.sizeToFit()
label.frame.size.width = viewWidth
return label
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: viewWidth, height: 1000))
view.backgroundColor = .white
let label1 = createLabel("\nfoo\n\nbar\n", .green)
let label2 = createLabel("\nfoo\n\nbar\n", .blue)
let label3 = createLabel("\nfoo\n\nbar\n\nfoo\n\nbar\n", .red)
let label4 = createLabel("\nfoo\n\nbar\n", .brown)
let label5 = createLabel("\nfoo\n\nbar\n", .brown)
label2.frame.origin.y = label1.frame.maxY
label3.frame.origin.y = label2.frame.maxY
label4.frame.origin.y = label3.frame.maxY
label5.frame.origin.y = label4.frame.maxY
view.addSubview(label1)
view.addSubview(label2)
view.addSubview(label3)
view.addSubview(label4)
view.addSubview(label5)
PlaygroundPage.current.liveView = view
/*
\nfoo\n\nbar\n
\nfoo\n\nbar\n
*/
when you combine the label4 and label5 that way, you have three new line,
the label4 should be let label4 = createLabel("\\nfoo\\n\\nbar", .brown)
remove \\n
at the end.
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.