NSRect 位置和尺寸
阿新 • • 發佈:2018-12-09
rect str 表示 size gre width 調用 函數 等價
前言
- 結構體 這個結構體用來表示事物的坐標點和寬高度。
typedef CGRect NSRect;
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
1、NSRect 結構體變量的創建與調用
// NSRect 結構體變量的創建與賦值 // 先定義變量,再賦值 NSRect rect1; rect1.origin.x = 6; rect1.origin.y = 1; rect1.size.width = 1; rect1.size.height = 6; // 定義時直接賦值,或者 NSRect rect2 = {{7, 2}, {2, 7}}; NSRect rect2 = {7, 2, 2, 7}; // 給指定成員賦值 NSRect rect3 = {.size.height = 8, .size.width = 3, .origin.y = 3, .origin.x = 8}; // 使用函數賦值 NSRect rect4 = NSMakeRect(9, 4, 4, 9); // 使用等價的結構體定義,等價於 CGRect rect5 = CGRectMake(10, 5, 5, 10); NSRect rect5 = CGRectMake(10, 5, 5, 10); // NSRect 結構體變量值的調用 NSLog(@"rect1: %.0f, %.0f, %.0f, %.0f", rect1.origin.x, rect1.origin.y, rect1.size.width, rect1.size.height); NSLog(@"rect2: %.0f, %.0f, %.0f, %.0f", rect2.origin.x, rect2.origin.y, rect2.size.width, rect2.size.height); NSLog(@"rect3: %.0f, %.0f, %.0f, %.0f", rect3.origin.x, rect3.origin.y, rect3.size.width, rect3.size.height); NSLog(@"rect4: %.0f, %.0f, %.0f, %.0f", rect4.origin.x, rect4.origin.y, rect4.size.width, rect4.size.height); NSLog(@"rect5: %.0f, %.0f, %.0f, %.0f", rect5.origin.x, rect5.origin.y, rect5.size.width, rect5.size.height);
2、NSRect 與 NSString 的相互轉換
// NSRect 轉 NSString
NSString *stringFronRect = NSStringFromRect(rect5);
// NSString 轉 NSRect
NSRect rect6 = NSRectFromString(stringFronRect);
NSRect 位置和尺寸