Objective-C中常用的結構體NSRange,NSPoint,NSSize(CGSize),NSRect

來源:互聯網
上載者:User

標籤:

1   NSRange

typedef struct _NSRange {
     NSUInteger location;
     NSUInteger length;
     } NSRange;
NSMakeRange 的函數

NS_INLINEz是內嵌函式
 
  typedef NSRange *NSRangePointer;
 
  NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) {
  NSRange r;
  r.location = loc;
  r.length = len;
  return r;
  }
使用方法

//NSRange表示的是範圍
  NSRange  range;
  range.location = 18;
  range.length = 34;
 
  NSLog(@"location is %zi",range.location);
  NSLog(@"length is %zi",range.length);
 
  //快速建立
  range = NSMakeRange(8, 10);
  NSLog(@"location is %zi",range.location);
  NSLog(@"length is %zi",range.length);
 
  //NSStringFromRange將上面的結構體轉化成字串類型,列印出來
 
  NSString* str1 = NSStringFromRange(range);
 
  //%@是一個OC對象,range代表的是一個結構體,str是一個OC對象
  NSLog(@"rang is %@",str1);
2  NSPoint

NSPoint的原型

struct CGPoint {
     CGFloat x;
     CGFloat y;
     };
NSMakePoint函數

NS_INLINE NSPoint NSMakePoint(CGFloat x, CGFloat y) {
  NSPoint p;
  p.x = x;
  p.y = y;
  return p;
  }
CGPointMake函數

CGPointMake(CGFloat x, CGFloat y)
     {
     CGPoint p; p.x = x; p.y = y; return p;
     }
使用方法

//NSPoint指的是位置
  NSPoint point;
 
  //給結構體裡面的點進行賦值
  point.x = 10;
  point.y = 10;
 
  //快速建立點
  point = NSMakePoint(10, 18);
 
  //常見的是CGPointMake建立點的函數
  point = CGPointMake(29, 78);
  NSString* str2 = NSStringFromPoint(point);
  NSLog(@"point is %@",str2);
3  CGSize

CGSize的原型

struct CGSize {
     CGFloat width;
     CGFloat height;
     };
NSMakeSize函數

NS_INLINE NSSize NSMakeSize(CGFloat w, CGFloat h) {
  NSSize s;
  s.width = w;
  s.height = h;
  return s;
  }
CGSizeMake函數

CGSizeMake(CGFloat width, CGFloat height)
     {
     CGSize size; size.width = width; size.height = height; return size;
     }
使用方法

NSSize size;
   
  size.width = 100;
  size.height = 12;
  size = NSMakeSize(12, 12);
  size = CGSizeMake(11, 11);
 
  NSString* str3 = NSStringFromSize(size);
  NSLog(@"%@",str3);
4 CGRect

CGRect的原型

struct CGRect {
     CGPoint origin;
     CGSize size;
     };
CGRectMake的函數

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函數

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;
  }
使用方法

//既包含了尺寸大小和位置
  NSRect rect;
  rect.origin.x = 12;
  rect.origin.y = 14;
  rect.size.width = 12;
  rect.size.height = 15;
 
  //快速建立方法
  rect = CGRectMake(12, 12, 12, 12);
  rect = NSMakeRect(11, 11, 11, 11);
 
  //轉化成字串列印出來
  NSString* str5 = NSStringFromRect(rect);
  NSLog(@"rect is %@",str5);

 

Objective-C中常用的結構體NSRange,NSPoint,NSSize(CGSize),NSRect

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.