I have recently started Beta Testing my SwiftUI App on my iPhone 6s, previously I was testing it on the iPhone 12 Simulator. While Testing on the Real Device I encountered issues like: CPU going past 98%
Really Laggy ScrollView
Tab Bar Freezes Randomly
Since My app is going to go in Production State in a few days, Since this is my first iOS App as well, I am not too sure if these issues are because of SwiftUI, my Phone or what, I have asked Apple Support but, haven't received a response so far.
My App Stats:
Lines of Code: ~1,500
CPU Usage on Simulator: Max. 10%
Bundle Size: 2MB
RAM Max. on Simulator: 32.3mb
Post not yet marked as solved
Hey,
When I use boundingRect with String, and supply .usesFontLeading as one of the options, I get an inaccurate height for some reason.
Why is that?
In the context of UILabel, If I were to calculate the height of the content I'm displaying... is it better to use textRect or boundingRect?
Post not yet marked as solved
I'm using boundingRect to calculate the height of a String, I'm using Ubuntu as my font, and when I supply .usesFontLeading as one of the options alongside .usesLineFragmentOrigin the height is completely different to what I expect.
Here is the code I'm using for reference:
func height(withConstrainedWidth width: CGFloat, font: UIFont, maxLines: CGFloat = 0) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: [
NSAttributedString.Key.font: font
], context: nil)
var size = boundingBox.size
if maxLines > 0 {
size.height = min(size.height, CGFloat(maxLines) * font.lineHeight)
}
return ceil(size.height)
}
Firstly, what does .usesFontLeading even do, and why is it that the result I'm getting an unexpected result.
Note: self is String because I've extended the String class.
I've constrained a UILabel to leading, and trailing anchors, the intrinsicContentSize exceeds the anchors I've specified. Just to be sure, I added a widthAnchor as well, still no improvement.
Then I added these properties:
self.preferredMaxLayoutWidth = 356.25 // didn't hard code
self.invalidateIntrinsicContentSize()
Still the intrinsicContentSize is equal to 356.5, which is something I don't expect from my UILabel, is there anyway to fix this issue?