WSTagsField
WSTagsField copied to clipboard
sizeToFit not implemented
Hi, I'm trying to calculate the height the WSTagsField needs in my view, any way to get that easily? I tried using sizeThatFits, but it seems not implemented and is returning 0:
let fullSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
var totalHeight: CGFloat = 0.0
totalHeight += titleLabel.sizeThatFits(fullSize).height // this works fine for a normal label
totalHeight += tagsField.sizeThatFits(fullSize).height // returns 0
return totalHeight
@Pe-te Yes, you're right. sizeThatFits
implementation is missing 😕 Let me see if I can fix that today.
Thanks for opening an issue.
Take your time, guess that's a tricky one!
For now I am using a workaround with onDidChangeHeightTo where I store the value in a global variable, but that feels really messy, so a clean solution would be very welcome.
@Pe-te Implemented eb54068ff23ec27326f73125c4962e7a6f3925c5.
Could you please test using the master
branch. Thanks!
Cool, will check first thing monday morning!
I'm not using insets, but don't you need to subtract left and right from the width? I could be wrong though, haven't looked at the code in detail yet.
@Pe-te That's a good point. Does the UILabel
have in consideration the insets for the width size?
You mean the UILabel titleLabel in my example above? I'm not setting any insets there, so it has the default insets. But it looks like you cannot just modify label insets by default anyway.
For WSTagsField I use the following insets. Works as expected, thanks!
// outer padding for whole tagsField
tagsField.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
// inner padding for every tag
tagsField.layoutMargins = UIEdgeInsets(top: 6, left: 12, bottom: 6, right: 12)
I also tried adding contentInsets for top and buttom, those work fine, even when you add like 40 tags.
But there are problems when you try to set left and right insets. For those the sizeToFit().height is way too large. This happens already with small amounts like 2-3 tags, but gets a lot worse for bigger amounts. Maybe an inverted sign?
I gave the view a backgroundcolor and it looks fine as expected, all paddings are applied correctly. Just sizeToFit() is acting weird.
Best guess: First all paddings need to be removed, then size calculation on the smaller canvas, then adding all four paddings again.
@Pe-te Thanks for the feedback. I'll look at it.