Reputation: 588
I need to know string height so I can set cell height. The problem is I am getting string asynchronous, and I need to know cell Height before creating collection view. And I need this to work with different languages (special characters).
String will be in a label inside the cell. I had some of my solutions, but one did not work with other languages, and the other did work but didnt get the real heigth.
Upvotes: 0
Views: 592
Reputation: 5058
use it o Swift 5
extension UILabel {
func calculateHeight() -> CGFloat {
let size = self.sizeThatFits(CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude))
return size.height
}
}
Upvotes: 0
Reputation: 6885
CGRect rect =[yourLabeltextString boundingRectWithSize:CGSizeMake(yourLabel.width, CGFLOAT_MAX)
options:(NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17]}
context:nil];
CGFloat height=rect.height;
Use the code above, and change the attributes to the one you prefered.You can get the height of your String.
Upvotes: 1
Reputation: 752
try this function and i hope it will help you :
- (CGFloat)calculateHeightForLabel:(UILabel *)label
{
CGRect frame = label.frame;
CGSize size = [label sizeThatFits:CGSizeMake(frame.size.width, FLT_MAX)];
return size.height;
}
Upvotes: 0