CGRectInset的使用

在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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值