Xamarin.iOS
Thanks to everyone for the answers provided above.
This gets the number of visible lines.
public static int VisibleLineCount(this UILabel label) { var textSize = new CGSize(label.Frame.Size.Width, nfloat.MaxValue); nfloat rHeight = label.SizeThatFits(textSize).Height; nfloat charSize = label.Font.LineHeight; return Convert.ToInt32(rHeight / charSize); }
It turns out the actual number of lines that the text will occupy on the screen.
public static int LineCount(this UILabel label) { var maxSize = new CGSize(label.Frame.Size.Width, nfloat.MaxValue); var charSize = label.Font.LineHeight; var text = (label.Text ?? "").ToNSString(); var textSize = text.GetBoundingRect(maxSize, NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes() { Font = label.Font }, null); return Convert.ToInt32(textSize.Height / charSize); }
A helper method that I find useful for my use case.
public static bool IsTextTruncated(this UILabel label) { if (label.Lines == 0) { return false; } return (label.LineCount() > label.Lines); }
To get a more accurate number of rows:
- Use
font.lineHeight instead of font.pointSize - round () number of rows after division
Baron Ch'ng Jun 23 '18 at 18:28 2018-06-23 18:28
source share