-2

how to get sum of two CGRect

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

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

CGRect height = ???

rmaddy
  • 314,917
  • 42
  • 532
  • 579
KkMIW
  • 1,092
  • 1
  • 17
  • 27
  • 2
    You need to be more explicit about what you're looking for - how should these 2 rects be combined? – Wain Jan 02 '15 at 20:23
  • requiredFirst.size.height + requiredLast.size.height; – KkMIW Jan 02 '15 at 20:25
  • 4
    @KkMIW: OK - you've just defined exactly the answer of the sum of heights of two rects. If you knew this already then why have you asked the question? – Robotic Cat Jan 02 '15 at 20:26
  • The height property would be a CGFloat, not a CGRect. – nanothread Jan 02 '15 at 20:44
  • I can't give you an answer because the question is closed, but the answer is in geometry.h, where there are a number of nice little functions, the one you want is CGRectUnion(rect1, rect2) which returns the smallest rect that contains rect1 & rect2.. https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGRectUnion – Jef Jan 02 '15 at 22:21
  • 1
    @Jef keep in mind that CGRectUnion will give a very different answer than the one provided by the accepted answer (and that answer doesn't seem useful at all). – rmaddy Jan 02 '15 at 22:23
  • heh, yup. Maybe even a result with some applications.. – Jef Jan 02 '15 at 22:28

2 Answers2

1

To get the sum of the heights:

CGFloat height = requiredFirst.size.height + requiredLast.size.height;
Chris
  • 7,270
  • 19
  • 66
  • 110
  • some times after posting question, i do get answer for my question. – KkMIW Jan 02 '15 at 20:30
  • 1
    @KkMIW Next time, take a few more minutes to think about what you're asking and see if you can come up with the solution on your own before posting a question then. Come to StackOverflow after you know you can't answer your own question :) – Chris Jan 02 '15 at 20:32
  • 1
    Shouldn't `height` be `CGFloat`, not `CGRect`? – rmaddy Jan 02 '15 at 20:33
  • Woops, you're right @rmaddy. I was basing it off OP's question and didn't think twice about the data type – Chris Jan 02 '15 at 20:35
1

Here's an example of a method that will do that:

- (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;
}

This method just adds all four of the frame's aspects (x, y, width, height) together individually then reconstructs it and returns it. Use it like this:

CGRect combinedFrame = [self combineFrames:requiredFirst, f2:requiredLast];
CGFloat height = combinedFrame.size.height;
nanothread
  • 908
  • 3
  • 10
  • 25