1

I am trying to create a custom cell which consists a few UILabels. The first label might take one or more rows, so I need to resize the label according to the number of lines (after setting the number of lines to 0, so multi-line will be enabled).

I have tried setting sizeToFit(), but it changed the alignment and width of my label.

I found this answer

but I don't know how to convert it to C#.

Can anyone point me to an example? (I already tried Googling it off-course)

This is the method from the link:

// UILabel *myLabel;

CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font 
                        constrainedToSize:myLabel.frame.size 
                            lineBreakMode:UILineBreakModeWordWrap];

CGFloat labelHeight = labelSize.height;


int lines = [myLabel.text sizeWithFont:myLabel.font 
                 constrainedToSize:myLabel.frame.size 
                     lineBreakMode:UILineBreakModeWordWrap].height/16; 
         // '16' is font size
Community
  • 1
  • 1
SuperFrog
  • 7,631
  • 9
  • 51
  • 81

1 Answers1

4
var size = myLabel.StringSize("Some really long string", myLabel.Font, myLabel.Frame.Size, UILineBreakMode.CharacterWrap);
var lines = size.Height / myLabel.Font.CapHeight;
Oliver Weichhold
  • 10,259
  • 5
  • 45
  • 87
  • When tried I get an error saying StringSize doesn't take 7 parameters. Checked in the assembly for the declaration: . public static CGSize StringSize (this NSString This, UIFont font, nfloat minFontSize, ref nfloat actualFontSize, nfloat forWidth, UILineBreakMode lineBreakMode); But it also mentioned its deprecated in ios 7.0 Any alternate solutions is much appreciated! Thanks. – coder284 Nov 18 '15 at 12:48