aksani56
aksani56

Reputation: 606

CGRectIntegral what is the usage of it?

How does one use the CGRectIntegral function? I understand it's purpose.

The documentation isn't clear on it's exact use.

Upvotes: 33

Views: 12271

Answers (3)

Hector
Hector

Reputation: 3907

CGRectIntegral to convert any decimal values to their integer equivalents
see image may be you can understand enter image description here

How do I fix it?

frame = CGRectIntegral(frame);

-OR-

myTextView.frame = CGRectIntegral(myTextView.frame);

  see this for more information of CGRectIntegral

Upvotes: 87

Vignesh
Vignesh

Reputation: 10251

CGRectIntegral Method is used to create integer rect . I mean to say suppose if you calculate frame in app you may get frames value in float.

Setting float value as frame to some UIElement like UILabel would make the text looks blur. To avoid that, we use CGRectIntegral.Please look at the example below,

NSLog(@"%@",NSStringFromCGRect(   CGRectIntegral(CGRectMake(0, 15.6, 16.1, 20.2))));

This will print, {0,15},{17,21}.

This explanation is found in the header file.

/* Expand `rect' to the smallest rect containing it with integral origin and
   size. */

Upvotes: 15

Mike Weller
Mike Weller

Reputation: 45598

One particular usage is to fix frames that do not align perfectly with on-screen pixels.

See this question: UITextField blurred text

If a label or textfield has a frame that isn't pixel-aligned when rendered to the screen, the text will appear blurred. This can happen if you calculate the frame using division (for example to center it in a parent view).

CGRectIntegral will remove the fractional part of the frame, fixing this problem. Note however that with retina displays a .5 frame value is pixel aligned, but CGRectIntgral will still remove the fractional part.

Upvotes: 8

Related Questions