We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi there Ricky,
What you think about its Swift implementation?
enum SPIconPosition { case SPIconPositionTop, SPIconPositionLeft, SPIconPositionBottom, SPIconPositionRight }
class SPButton {
//CGFloat iconMargin; //NSInteger iconPosition; //CGSize iconSize; // default is
func commonInit() { self.iconSize = CGSizeZero } init(aDecoder: NSCoder) { self = super(coder: aDecoder) if self { self.commonInit() } return self } init(frame: CGRect) { self = super(frame: frame) if self { self.commonInit() } return self } func titleRectForContentRect(contentRect: CGRect) -> CGRect { // clang diagnostic push // clang diagnostic ignored "-Wdeprecated" var size: CGSize = self.titleForState(self.state).sizeWithFont(self.font, constrainedToSize: contentRect.size) // clang diagnostic pop var iconSize: CGSize = CGSizeEqualToSize(self.iconSize, CGSizeZero) ? super.imageRectForContentRect(contentRect).size : self.iconSize var totalWidth: CGFloat = size.width+iconSize.width+self.iconMargin var totalHeight: CGFloat = size.height+iconSize.height+self.iconMargin var rect: CGRect = { switch self.contentHorizontalAlignment { case UIControlContentHorizontalAlignmentLeft: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMinX(contentRect) break case .Left: rect.origin.x = CGRectGetMinX(contentRect)+totalWidth-size.width break default: rect.origin.x = CGRectGetMinX(contentRect) break } break case UIControlContentHorizontalAlignmentRight: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMaxX(contentRect)-totalWidth break case .Left: rect.origin.x = CGRectGetMaxX(contentRect)-size.width break default: rect.origin.x = CGRectGetMaxX(contentRect)-size.width break } break case UIControlContentHorizontalAlignmentFill: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMinX(contentRect) break case .Left: rect.origin.x = CGRectGetMaxX(contentRect)-size.width break default: rect.origin.x = CGRectGetMinX(contentRect)+(CGRectGetWidth(contentRect)-size.width)/2 break } break default: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMinX(contentRect)+(CGRectGetWidth(contentRect)-totalWidth)/2 break case .Left: rect.origin.x = CGRectGetMinX(contentRect)+CGRectGetWidth(contentRect)-(CGRectGetWidth(contentRect)-totalWidth)/2-size.width break default: rect.origin.x = CGRectGetMinX(contentRect)+(CGRectGetWidth(contentRect)-size.width)/2 break } break } switch self.contentVerticalAlignment { case UIControlContentVerticalAlignmentTop: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMinY(contentRect)+totalHeight-size.height break case .Bottom: rect.origin.y = CGRectGetMinY(contentRect) break default: rect.origin.y = CGRectGetMinY(contentRect) break } break case UIControlContentVerticalAlignmentBottom: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMaxY(contentRect)-size.height break case .Bottom: rect.origin.y = CGRectGetMaxY(contentRect)-totalHeight break default: rect.origin.y = CGRectGetMaxY(contentRect)-size.height break } break case UIControlContentVerticalAlignmentFill: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMaxY(contentRect)-size.height break case .Bottom: rect.origin.y = CGRectGetMinY(contentRect) break default: rect.origin.y = CGRectGetMinY(contentRect)+(CGRectGetHeight(contentRect)-size.height)/2 break } break default: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMaxY(contentRect)-(CGRectGetHeight(contentRect)-totalHeight)/2-size.height break case .Bottom: rect.origin.y = CGRectGetMinY(contentRect)+(CGRectGetHeight(contentRect)-totalHeight)/2 break default: rect.origin.y = CGRectGetMinY(contentRect)+(CGRectGetHeight(contentRect)-size.height)/2 break } break } return rect } func imageRectForContentRect(contentRect: CGRect) -> CGRect { var size: CGSize = CGSizeEqualToSize(self.iconSize, CGSizeZero) ? super.imageRectForContentRect(contentRect).size : self.iconSize var titleSize: CGSize = self.titleRectForContentRect(contentRect).size switch _iconPosition { case .Top: case .Bottom: size.height = MAX(MIN(CGRectGetHeight(contentRect)-self.iconMargin-titleSize.height, size.height), self.iconSize.height) break default: size.width = MAX(MIN(CGRectGetWidth(contentRect)-self.iconMargin-titleSize.width, size.width), self.iconSize.width) break } var totalWidth: CGFloat = size.width+titleSize.width+self.iconMargin var totalHeight: CGFloat = size.height+titleSize.height+self.iconMargin var rect: CGRect = { switch self.contentHorizontalAlignment { case UIControlContentHorizontalAlignmentLeft: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMinX(contentRect)+totalWidth-size.width break case .Left: rect.origin.x = CGRectGetMinX(contentRect) break default: rect.origin.x = CGRectGetMinX(contentRect) break } break case UIControlContentHorizontalAlignmentRight: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMaxX(contentRect)-size.width break case .Left: rect.origin.x = CGRectGetMaxX(contentRect)-totalWidth break default: rect.origin.x = CGRectGetMaxX(contentRect)-size.width break } break case UIControlContentHorizontalAlignmentFill: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMaxX(contentRect)-size.width break case .Left: rect.origin.x = CGRectGetMinX(contentRect) break default: rect.origin.x = CGRectGetMinX(contentRect)+(CGRectGetWidth(contentRect)-size.width)/2 break } break default: switch _iconPosition { case .Right: rect.origin.x = CGRectGetMinX(contentRect)+CGRectGetWidth(contentRect)-(CGRectGetWidth(contentRect)-totalWidth)/2-size.width break case .Left: rect.origin.x = CGRectGetMinX(contentRect)+(CGRectGetWidth(contentRect)-totalWidth)/2 break default: rect.origin.x = CGRectGetMinX(contentRect)+(CGRectGetWidth(contentRect)-size.width)/2 break } break } switch self.contentVerticalAlignment { case UIControlContentVerticalAlignmentTop: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMinY(contentRect) break case .Bottom: rect.origin.y = CGRectGetMinY(contentRect)+totalHeight-size.height break default: rect.origin.y = CGRectGetMinY(contentRect) break } break case UIControlContentVerticalAlignmentBottom: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMaxY(contentRect)-totalHeight break case .Bottom: rect.origin.y = CGRectGetMaxY(contentRect)-size.height break default: rect.origin.y = CGRectGetMaxY(contentRect)-size.height break } break case UIControlContentVerticalAlignmentFill: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMinY(contentRect) break case .Bottom: rect.origin.y = CGRectGetMaxY(contentRect)-size.height break default: rect.origin.y = CGRectGetMinY(contentRect)+(CGRectGetHeight(contentRect)-size.height)/2 break } break default: switch _iconPosition { case .Top: rect.origin.y = CGRectGetMinY(contentRect)+(CGRectGetHeight(contentRect)-totalHeight)/2 break case .Bottom: rect.origin.y = CGRectGetMaxY(contentRect)-(CGRectGetHeight(contentRect)-totalHeight)/2-size.height break default: rect.origin.y = CGRectGetMinY(contentRect)+(CGRectGetHeight(contentRect)-size.height)/2 break } break } return rect } func intrinsicContentSize() -> CGSize { var contentRect: CGRect = self.contentRectForBounds(self.bounds) // clang diagnostic push // clang diagnostic ignored "-Wdeprecated" var titleSize: CGSize = self.titleForState(self.state).sizeWithFont(self.font) // clang diagnostic pop var imageSize: CGSize = CGSizeEqualToSize(self.iconSize, CGSizeZero) ? super.imageRectForContentRect(contentRect).size : self.iconSize switch _iconPosition { case .Top: case .Bottom: return CGSizeMake(MAX(titleSize.width, imageSize.width), titleSize.height+imageSize.height+self.iconMargin) break default: return CGSizeMake(titleSize.width+imageSize.width+self.iconMargin, MAX(titleSize.height, imageSize.height)) break } } func sizeThatFits(size: CGSize) -> CGSize { return self.intrinsicContentSize() }
}
The text was updated successfully, but these errors were encountered:
I'm not so familiar with Swift for now, but I think you can try convert to swift in Xcode
Swift
Sorry, something went wrong.
No branches or pull requests
Hi there Ricky,
What you think about its Swift implementation?
enum SPIconPosition {
case SPIconPositionTop, SPIconPositionLeft, SPIconPositionBottom, SPIconPositionRight
}
class SPButton {
//CGFloat iconMargin;
//NSInteger iconPosition;
//CGSize iconSize; // default is
}
The text was updated successfully, but these errors were encountered: