两个CGRect的总和

时间:2015-01-02 20:19:54

标签: ios objective-c cgrect

如何获得两个CGRect的总和

CGRect requiredFirst = [firstName boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

CGRect requiredLast = [lastName boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

CGRect height = ???

2 个答案:

答案 0 :(得分:1)

获得高度的总和:

CGFloat height = requiredFirst.size.height + requiredLast.size.height;

答案 1 :(得分:0)

以下是一个方法示例:

- (CGRect)combineFrames:(CGRect)frame1, f2:(CGRect)frame2{
    CGFloat x = frame1.origin.x + frame2.origin.x;
    CGFloat y = frame1.origin.y + frame2.origin.y;
    CGFloat width = frame1.size.width + frame2.size.width;
    CGFloat height = frame1.size.height + frame2.size.height;

    CGRect combinedFrame = CGRectMake(x, y, width, height);
    return combinedFrame;
}

此方法只是将框架的所有四个方面(x,y,宽度,高度)分别加在一起,然后重新构建并返回它。 像这样使用它:

CGRect combinedFrame = [self combineFrames:requiredFirst, f2:requiredLast];
CGFloat height = combinedFrame.size.height;