This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from indragiek/master
MEDEdgeInsets
- Loading branch information
Showing
8 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// MEDEdgeInsets.h | ||
// Archimedes | ||
// | ||
// Created by Indragie Karunaratne on 8/6/2013. | ||
// Copyright (c) 2013 GitHub. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED | ||
#import <UIKit/UIKit.h> | ||
typedef UIEdgeInsets MEDEdgeInsets; | ||
#elif TARGET_OS_MAC | ||
#import <AppKit/AppKit.h> | ||
typedef NSEdgeInsets MEDEdgeInsets; | ||
#endif | ||
|
||
// `MEDEdgeInsets` structure with all members set to 0. | ||
#define MEDEdgeInsetsZero (MEDEdgeInsets){ .top = 0, .left = 0, .bottom = 0, .right = 0 } | ||
|
||
// Returns an MEDEgeInsets struct with the given edge insets. | ||
MEDEdgeInsets MEDEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right); | ||
|
||
// Returns whether the two given `MEDEdgeInsets` are equal. | ||
BOOL MEDEdgeInsetsEqualToEdgeInsets(MEDEdgeInsets insets1, MEDEdgeInsets insets2); | ||
|
||
// The top inset will affect the min Y coordinate on iOS, and max Y coordinate on | ||
// OS X, and vice-versa for bottom due to the default flippedness of drawing contexts | ||
// on each platform. | ||
// | ||
// Returns a rectangle adjusted by incrementing the origin and decrementing the size | ||
// of the given rect by applying the given insets. | ||
CGRect MEDEdgeInsetsInsetRect(CGRect rect, MEDEdgeInsets insets); | ||
|
||
// Returns a string formatted to contain data from an `MEDEdgeInsets` structure. | ||
// | ||
// This string can be passed into `MEDEdgeInsetsFromString()` to recreate the original | ||
// `MEDEdgeInsets` structure. | ||
NSString * NSStringFromMEDEdgeInsets(MEDEdgeInsets insets); | ||
|
||
// Returns an `MEDEdgeInsets` structure corresponding to data in the given string | ||
// or `MEDEdgeInsetsZero` if the string is not formatted appropriately. | ||
// | ||
// The string format is “{top, left, bottom, right}”, where each member of the | ||
// `MEDEdgeInsets` structure is separated with a comma. This function should generally | ||
// only be used to convert strings that were previously created using the | ||
// `NSStringFromMEDEdgeInsets()` function. | ||
MEDEdgeInsets MEDEdgeInsetsFromString(NSString *string); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// MEDEdgeInsets.m | ||
// Archimedes | ||
// | ||
// Created by Indragie Karunaratne on 8/6/2013. | ||
// Copyright (c) 2013 GitHub. All rights reserved. | ||
// | ||
|
||
#import "MEDEdgeInsets.h" | ||
|
||
MEDEdgeInsets MEDEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) { | ||
return (MEDEdgeInsets){ .top = top, .left = left, .bottom = bottom, .right = right }; | ||
} | ||
|
||
BOOL MEDEdgeInsetsEqualToEdgeInsets(MEDEdgeInsets insets1, MEDEdgeInsets insets2) { | ||
return (fabs(insets1.top - insets2.top) < 0.1) && | ||
(fabs(insets1.left - insets2.left) < 0.1) && | ||
(fabs(insets1.bottom - insets2.bottom) < 0.1) && | ||
(fabs(insets1.right - insets2.right) < 0.1); | ||
} | ||
|
||
CGRect MEDEdgeInsetsInsetRect(CGRect rect, MEDEdgeInsets insets) { | ||
if (((insets.top + insets.bottom) > rect.size.height) || ((insets.left + insets.right) > rect.size.width)) { | ||
return CGRectNull; | ||
} else { | ||
rect.origin.x += insets.left; | ||
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED | ||
rect.origin.y += insets.top; | ||
#elif TARGET_OS_MAC | ||
rect.origin.y += insets.bottom; | ||
#endif | ||
rect.size.height -= (insets.top + insets.bottom); | ||
rect.size.width -= (insets.left + insets.right); | ||
return rect; | ||
} | ||
} | ||
|
||
NSString * NSStringFromMEDEdgeInsets(MEDEdgeInsets insets) { | ||
return [NSString stringWithFormat:@"{%g, %g, %g, %g}", insets.top, insets.left, insets.bottom, insets.right]; | ||
} | ||
|
||
MEDEdgeInsets MEDEdgeInsetsFromString(NSString *string) { | ||
NSCParameterAssert(string != nil); | ||
double top = 0, left = 0, bottom = 0, right = 0; | ||
sscanf(string.UTF8String, "{%lg, %lg, %lg, %lg}", &top, &left, &bottom, &right); | ||
return MEDEdgeInsetsMake((CGFloat)top, (CGFloat)left, (CGFloat)bottom, (CGFloat)right); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// MEDEdgeInsetsSpec.m | ||
// Archimedes | ||
// | ||
// Created by Indragie Karunaratne on 8/6/2013. | ||
// Copyright (c) 2013 GitHub. All rights reserved. | ||
// | ||
|
||
#import "MEDEdgeInsets.h" | ||
|
||
static const MEDEdgeInsets insets = (MEDEdgeInsets){ .top = 1, .left = 2, .bottom = 3, .right = 4 }; | ||
static const MEDEdgeInsets insets2 = (MEDEdgeInsets){ .top = 1.05f, .left = 2.05f, .bottom = 3.05f, .right = 4.05f }; | ||
|
||
SpecBegin(MEDEdgeInsets) | ||
|
||
it(@"should check equality between MEDEdgeInsets", ^{ | ||
MEDEdgeInsets insets3 = MEDEdgeInsetsMake(5, 6, 7, 8); | ||
expect(MEDEdgeInsetsEqualToEdgeInsets(insets, insets)).to.beTruthy(); | ||
expect(MEDEdgeInsetsEqualToEdgeInsets(insets, insets2)).to.beTruthy(); | ||
expect(MEDEdgeInsetsEqualToEdgeInsets(insets, insets3)).to.beFalsy(); | ||
}); | ||
|
||
it(@"should make an MEDEdgeInsets", ^{ | ||
expect(MEDEdgeInsetsMake(1, 2, 3, 4)).to.equal(insets); | ||
}); | ||
|
||
it(@"should inset a CGRect", ^{ | ||
CGRect rect = CGRectMake(10, 10, 10, 10); | ||
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED | ||
CGRect insetRect = CGRectMake(12, 11, 4, 6); | ||
#elif TARGET_OS_MAC | ||
CGRect insetRect = CGRectMake(12, 13, 4, 6); | ||
#endif | ||
expect(MEDEdgeInsetsInsetRect(rect, insets)).to.equal(insetRect); | ||
}); | ||
|
||
it(@"should create a string from an MEDEdgeInsets", ^{ | ||
expect(NSStringFromMEDEdgeInsets(insets)).to.equal(@"{1, 2, 3, 4}"); | ||
expect(NSStringFromMEDEdgeInsets(insets2)).to.equal(@"{1.05, 2.05, 3.05, 4.05}"); | ||
}); | ||
|
||
it(@"should create an MEDEdgeInsets from a string", ^{ | ||
expect(MEDEdgeInsetsFromString(@"{1, 2, 3, 4}")).to.equal(insets); | ||
expect(MEDEdgeInsetsEqualToEdgeInsets(MEDEdgeInsetsFromString(@"{1.05, 2.05, 3.05, 4.05}"), insets2)).to.beTruthy(); | ||
}); | ||
|
||
SpecEnd |
Oops, something went wrong.