Calculating UILabel height and UIFont height (for number of lines) using ceil () or roundf ()?

I have these values ​​that I registered:

label.frame.size.height :18.000000, label.font.lineHeight: 17.895000

      

if i use roundf () like:

roundf(label.frame.size.height / label.font.lineHeight) // answer: 1

      

whereas with ceil ()

ceil(label.frame.size.height / label.font.lineHeight) // answer: 2

      

but when calculating manually: the answer is 1.00586756

I wonder what is better and more reliable (in general) between the two. Why does everyone use ceil()

to determine the number of lines UILabel

?

+3


source to share


2 answers


I still don't understand why people should use ceil()

when calculating the number of rows as roundf()

more accurate.

But when it comes to calculating for the number of lines ... I look at me that "roundf ()" is indeed more accurate, but since its number of lines ... the decimal values ​​are irrelevant. enter image description here

Image calculation:

54 / 17.895000 = 3.01760268

And numberOflines = 3

if we use roundf()

, the answer is 3

, and



and if ceil()

already4

so using floor()

or just converting the result to int

will do the job:

int result = (int)floor(answer);

//or

int result = (int)answer;

      

About my question, I think it roundf()

works for me to calculate the number of lines in general.

I am making a class that will calculate the number of rows from these values ​​and will be used by the whole application.

0


source


In the case of the number of lines, every letter after the limit that the line can display must wrap to the next line, so .005 is also significant. This part of the text should wrap to the next line. So it's better to use ceil () rather than roundf (). In roundf (), the value will only be significant when it is greater than or equal to half of the value)

CEIL ()



The C library function ceil (x) returns the smallest integer value greater than or equal to x.

+2


source







All Articles