Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions SYFlatButton/SYFlatButton/SYFlatButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ - (void)setupImageLayer {
CGFloat y = 0.0; // Image's origin y

// Caculate the image's and title's position depends on button's imagePosition and imageHugsTitle property
switch (self.imagePosition) {

NSCellImagePosition effectiveImagePosition = [self effectiveImagePositionFrom:self.imagePosition];
BOOL effectiveImageHugsTitle = NO;
if (@available(macOS 10.12, *)) {
effectiveImageHugsTitle = self.imageHugsTitle;
}

switch (effectiveImagePosition) {
case NSNoImage:
return;
break;
Expand All @@ -94,26 +101,24 @@ - (void)setupImageLayer {
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageLeading:
case NSImageLeft: {
x = self.imageHugsTitle ? ((buttonSize.width - imageSize.width - titleSize.width) / 2.0 - self.spacing) : self.spacing;
x = effectiveImageHugsTitle ? ((buttonSize.width - imageSize.width - titleSize.width) / 2.0 - self.spacing) : self.spacing;
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageTrailing:
case NSImageRight: {
x = self.imageHugsTitle ? ((buttonSize.width - imageSize.width + titleSize.width) / 2.0 + self.spacing) : (buttonSize.width - imageSize.width - self.spacing);
x = effectiveImageHugsTitle ? ((buttonSize.width - imageSize.width + titleSize.width) / 2.0 + self.spacing) : (buttonSize.width - imageSize.width - self.spacing);
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageAbove: {
x = (buttonSize.width - imageSize.width) / 2.0;
y = self.imageHugsTitle ? ((buttonSize.height - imageSize.height - titleSize.height) / 2.0 - self.spacing) : self.spacing;
y = effectiveImageHugsTitle ? ((buttonSize.height - imageSize.height - titleSize.height) / 2.0 - self.spacing) : self.spacing;
break;
}
case NSImageBelow: {
x = (buttonSize.width - imageSize.width) / 2.0;
y = self.imageHugsTitle ? ((buttonSize.height - imageSize.height + titleSize.height) / 2.0 + self.spacing) : (buttonSize.height - imageSize.height - self.spacing);
y = effectiveImageHugsTitle ? ((buttonSize.height - imageSize.height + titleSize.height) / 2.0 + self.spacing) : (buttonSize.height - imageSize.height - self.spacing);
break;
}
default: {
Expand Down Expand Up @@ -147,7 +152,14 @@ - (void)setupTitleLayer {
CGFloat y = 0.0; // Title's origin y

// Caculate the image's and title's position depends on button's imagePosition and imageHugsTitle property
switch (self.imagePosition) {

NSCellImagePosition effectiveImagePosition = [self effectiveImagePositionFrom:self.imagePosition];
BOOL effectiveImageHugsTitle = NO;
if (@available(macOS 10.12, *)) {
effectiveImageHugsTitle = self.imageHugsTitle;
}

switch (effectiveImagePosition) {
case NSImageOnly: {
return;
break;
Expand All @@ -162,25 +174,23 @@ - (void)setupTitleLayer {
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageLeading:
case NSImageLeft: {
x = self.imageHugsTitle ? ((buttonSize.width + imageSize.width - titleSize.width) / 2.0 + self.spacing) : (buttonSize.width - titleSize.width - self.spacing);
x = effectiveImageHugsTitle ? ((buttonSize.width + imageSize.width - titleSize.width) / 2.0 + self.spacing) : (buttonSize.width - titleSize.width - self.spacing);
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageTrailing:
case NSImageRight: {
x = self.imageHugsTitle ? ((buttonSize.width - imageSize.width - titleSize.width) / 2.0 - self.spacing) : self.spacing;
x = effectiveImageHugsTitle ? ((buttonSize.width - imageSize.width - titleSize.width) / 2.0 - self.spacing) : self.spacing;
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageAbove: {
x = (buttonSize.width - titleSize.width) / 2.0;
y = self.imageHugsTitle ? ((buttonSize.height + imageSize.height - titleSize.height) / 2.0 + self.spacing) : (buttonSize.height - titleSize.height - self.spacing);
y = effectiveImageHugsTitle ? ((buttonSize.height + imageSize.height - titleSize.height) / 2.0 + self.spacing) : (buttonSize.height - titleSize.height - self.spacing);
break;
}
case NSImageBelow: {
y = self.imageHugsTitle ? ((buttonSize.height - imageSize.height - titleSize.height) / 2.0 - self.spacing) : self.spacing;
y = effectiveImageHugsTitle ? ((buttonSize.height - imageSize.height - titleSize.height) / 2.0 - self.spacing) : self.spacing;
x = (buttonSize.width - titleSize.width) / 2.0;
break;
}
Expand Down Expand Up @@ -378,4 +388,26 @@ - (CATextLayer *)titleLayer {
return _titleLayer;
}

#pragma mark - Helper Methods

- (NSCellImagePosition)effectiveImagePositionFrom:(NSCellImagePosition) originalImagePosition {

NSCellImagePosition effectiveImagePosition = originalImagePosition;

if (@available(macOS 10.12, *)) {
switch(originalImagePosition) {
case NSImageTrailing:
effectiveImagePosition = NSImageRight;
break;
case NSImageLeading:
effectiveImagePosition = NSImageLeft;
break;
default:
//no mapping needed
break;
}
}
return effectiveImagePosition;
}

@end