Common structural Body nsrange,nspoint,nssize (cgsize) in objective-c, Nsrect case Analysis _c language

Source: Internet
Author: User

In this paper, the definition and usage of the structural body nsrange,nspoint,nssize (cgsize) and Nsrect in Objective-c are described in detail, as follows:

1, Nsrange:

Nsrange's prototype is

typedef struct _NSRANGE {
nsuinteger location;
Nsuinteger length;
} Nsrange;

Nsmakerange functions:

Ns_inlinez is an inline function

typedef nsrange *nsrangepointer;

Ns_inline nsrange nsmakerange (Nsuinteger Loc, Nsuinteger len) {
nsrange R;
r.location = loc;
R.length = len;
return r;
}

How to use:

The Nsrange represents the range of
nsrange ranges;
Range.location =;
Range.length =;

NSLog (@ "Location is%zi", range.location);
NSLog (@ "Length is%zi", range.length);

Quick Create
range = Nsmakerange (8);
NSLog (@ "Location is%zi", range.location);
NSLog (@ "Length is%zi", range.length);

Nsstringfromrange converts the above structure to a string type and prints out

nsstring* str1 = nsstringfromrange (range);

%@ is an OC object, range represents a struct, and Str is an OC object
NSLog (@ "Rang is%@", str1);

2, Nspoint:

Nspoint's prototype:

struct Cgpoint {
cgfloat x;
CGFloat y;
};

Nsmakepoint function:

Ns_inline nspoint Nsmakepoint (cgfloat x, cgfloat y) {
nspoint p;
p.x = x;
P.y = y;
return p;
}

Cgpointmake function:

Cgpointmake (CGFloat x, cgfloat y)
{
Cgpoint p; p.x = x; p.y = y; return p;
}

How to use:

Nspoint refers to the position
Nspoint point;

Assigning value to the points in the structure
point.x = ten;
Point.y = ten;

Quick Create dot point
= Nsmakepoint (a);

It is common for cgpointmake to create points of function point
= Cgpointmake (s);
nsstring* str2 = Nsstringfrompoint (point);
NSLog (@ "point is%@", str2);

3, Cgsize:

Cgsize's prototype:

struct Cgsize {
cgfloat width;
CGFloat height;
};

Nsmakesize function:

Ns_inline nssize nsmakesize (cgfloat W, cgfloat h) {
nssize s;
S.width = W;
S.height = h;
return s;
}

Cgsizemake function:

Cgsizemake (cgfloat width, cgfloat height)
{
cgsize size; size.width = width; size.height = height; return size;
   }

How to use:

Nssize size;
  
Size.width = m;
Size.Height =;
Size = Nsmakesize (a);
Size = Cgsizemake (one by one);

nsstring* STR3 = nsstringfromsize (size);
NSLog (@ "%@", STR3);

4, CGRect:

CGRect's prototype:

struct CGRect {
cgpoint origin;
Cgsize size;
};

CGRectMake functions:

CGRectMake (CGFloat x, cgfloat y, cgfloat width, cgfloat height)
{
cgrect rect;
rect.origin.x = x; Rect.origin.y = y;
Rect.size.width = width; rect.size.height = height;
return rect;
}

Nsmakerect function:

Ns_inline nsrect Nsmakerect (cgfloat x, CGFloat y, CGFloat W, CGFloat h) {
nsrect R;
r.origin.x = x;
R.origin.y = y;
R.size.width = W;
R.size.height = h;
return r;
}

How to use

It includes both size and position
nsrect rect;
Rect.origin.x =;
RECT.ORIGIN.Y = n;
Rect.size.width =;
Rect.size.height =;

Quick Create method
rect = CGRectMake (a)
Rect = Nsmakerect (one, one, one, one);

into a string to print out
nsstring* STR5 = nsstringfromrect (rect);
NSLog (@ "rect is%@", STR5);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.