Can I adjust a CGRect with a UIEdgeInsets?
Asked Answered
A

4

45

I've got a CGRect, and I'd like to adjust it with a UIEdgeInsets.

It seems like perhaps there might be a built in function that does this. I've looked for a CGRectAdjustByInsets or functions with some other CGRect… prefix, but I didn't find anything.

Should I code my own?

Anthracoid answered 2/1, 2015 at 11:44 Comment(0)
A
97

TL;DR

Swift 4.2 use theRect.inset(by: theInsets).

Objective c use UIEdgeInsetsInsetRect(theRect, theInsets)

Example

// CGRectMake takes: left, bottom, width, height.
const CGRect originalRect = CGRectMake(0, 0, 100, 50);

// UIEdgeInsetsMake takes: top, left, bottom, right.
const UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, -20, -20);

// Apply the insets…
const CGRect adjustedRect = UIEdgeInsetsInsetRect(originalRect, insets);

// What's the result?
NSLog(@"%@ inset by %@ is %@", 
      NSStringFromCGRect(originalRect),
      NSStringFromUIEdgeInsets(insets),
      NSStringFromCGRect(adjustedRect));

// Logs out…
// {{0, 0}, {100, 50}} inset by {10, 10, -20, -20} is {{10, 10}, {110, 60}}

Explanation

  • A positive inset moves the rectangle's edge inwards (towards the rectangle middle).
  • A negative inset moves the edge outward (away from the rectangle middle).
  • A zero inset will leaves the edge alone.

Tell Me More

Further useful functions for operating on CGRects are covered by this note.

Anthracoid answered 2/1, 2015 at 11:45 Comment(1)
Fantastic! I should've expected this method to exist, but I am grateful that I did not have to search hard for it! ;-)Carolann
N
8

2018 ... Swift4

Say you want the bounds,

but for example less two pixels on the bottom:

let ei = UIEdgeInsetsMake(0, 0, 2, 0)   // top-left-bottom-right
let smaller = UIEdgeInsetsInsetRect(bounds, ei)

That's it.

If you prefer to write it as one line, it's just

Take two off the bottom:

let newBounds = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 2, 0))

Cheers

Napery answered 4/4, 2017 at 3:20 Comment(4)
It's unusual that Swift 3 didn't also include this as a function on CGRect: rect.insetBy(edgeInset) or something like that.Carnatic
No, I mean, the syntax. Rather than it being a verbose function, it could just operate on a CGRect directly.Carnatic
I'm aware. But it could be. Just like all the other CGRect functions.Carnatic
actually - you're quite right, Lucas. Like say .union is a good example.Napery
S
7

Still 2018 ... Swift 4.2

I guess the new way looks better...

let newCGRect = oldCGRect.inset(by: UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8))
Script answered 1/8, 2018 at 22:2 Comment(0)
S
0
CGRect insetRect = CGRectInset(rOriginalRect, fInsetX, fInsetY);
Steersman answered 6/9, 2020 at 23:31 Comment(1)
Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues.Excipient

© 2022 - 2024 — McMap. All rights reserved.