在IntroduceToTextKitDemo中,在添加textView视图时使用了CGRectInset来定义其框架:
CGRect newTextViewRect = CGRectInset(self.view.bounds, 8., 0.);
UITextView *newTextView = [[UITextView alloc] initWithFrame:newTextViewRect textContainer:container];
这是sdk包中的定义:
/* Inset `rect' by `(dx, dy)' -- i.e., offset its origin by `(dx, dy)', and
decrease its size by `(2*dx, 2*dy)'. */
CG_EXTERN CGRect CGRectInset(CGRect rect, CGFloat dx, CGFloat dy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
随后我写了一个Demo来试用一下,代码如下
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = CGRectInset(self.view.bounds, 8., 0.);
UIView *greenView = [[UIView alloc] initWithFrame:rect];
greenView.backgroundColor = [UIColor greenColor];
NSLog(@"greenView location:");
NSLog(@"x = %f", greenView.frame.origin.x);
NSLog(@"y = %f", greenView.frame.origin.y);
NSLog(@"width = %f", greenView.frame.size.width);
NSLog(@"height = %f", greenView.frame.size.height);
CGRect newRect0 = CGRectInset(greenView.frame, 0., 100.0);
UIView *blueView = [[UIView alloc] initWithFrame:newRect0];
blueView.backgroundColor = [UIColor blueColor];
NSLog(@"blueView location:");
NSLog(@"x = %f", blueView.frame.origin.x);
NSLog(@"y = %f", blueView.frame.origin.y);
NSLog(@"width = %f", blueView.frame.size.width);
NSLog(@"height = %f", blueView.frame.size.height);
[greenView addSubview:blueView]; // 以greenView为参考坐标系添加blueView
CGRect newRect = CGRectInset(greenView.bounds, 0., 200.0);
UIView *blackView = [[UIView alloc] initWithFrame:newRect];
blackView.backgroundColor = [UIColor blackColor];
NSLog(@"blackView location:");
NSLog(@"x = %f", blackView.frame.origin.x);
NSLog(@"y = %f", blackView.frame.origin.y);
NSLog(@"width = %f", blackView.frame.size.width);
NSLog(@"height = %f", blackView.frame.size.height);
[greenView addSubview:blackView];
[self.view addSubview:greenView];
}