【发布时间】:2015-01-02 11:44:30
【问题描述】:
我有一个CGRect,我想用UIEdgeInsets 调整它。
似乎可能有一个内置函数可以做到这一点。我一直在寻找 CGRectAdjustByInsets 或带有其他 CGRect… 前缀的函数,但没有找到任何东西。
我应该自己编码吗?
【问题讨论】:
标签: cgrect uiedgeinsets
我有一个CGRect,我想用UIEdgeInsets 调整它。
似乎可能有一个内置函数可以做到这一点。我一直在寻找 CGRectAdjustByInsets 或带有其他 CGRect… 前缀的函数,但没有找到任何东西。
我应该自己编码吗?
【问题讨论】:
标签: cgrect uiedgeinsets
Swift 4.2 使用theRect.inset(by: theInsets)。
目标c使用UIEdgeInsetsInsetRect(theRect, theInsets)
// 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}}
this note 涵盖了在 CGRects 上操作的更多有用功能。
【讨论】:
说你想要边界,
但例如在底部上少于两个像素:
let ei = UIEdgeInsetsMake(0, 0, 2, 0) // top-left-bottom-right
let smaller = UIEdgeInsetsInsetRect(bounds, ei)
就是这样。
从底部取下两个:
let newBounds = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 2, 0))
干杯
【讨论】:
CGRect:rect.insetBy(edgeInset) 或类似的东西上包含这个函数。
CGRect 进行操作。
.union就是一个很好的例子。
我猜新方法看起来更好......
let newCGRect = oldCGRect.inset(by: UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8))
【讨论】:
CGRect insetRect = CGRectInset(rOriginalRect, fInsetX, fInsetY);
【讨论】: