Skip to content

Commit

Permalink
Fixed button issue codestergit#24
Browse files Browse the repository at this point in the history
Re-did the button calculation so it looks better and width doesn’t
extend when title is too long. Now title truncates as one would expect.
  • Loading branch information
Ace Green authored and Ace Green committed Sep 21, 2015
1 parent 058ee10 commit dbc29e7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions SweetAlert.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
9B4CD1001A06E55200B65DE0 /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Sahil Wasan";
TargetAttributes = {
Expand Down
Binary file not shown.
41 changes: 18 additions & 23 deletions SweetAlert/SweetAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SweetAlert: UIViewController {
let kWidthMargin: CGFloat = 10.0
let kAnimatedViewHeight: CGFloat = 70.0
let kMaxHeight: CGFloat = 300.0
var kContentWidth: CGFloat = 300.0
var kMaxWidth: CGFloat = 300.0
let kButtonHeight: CGFloat = 35.0
var textViewHeight: CGFloat = 90.0
let kTitleHeight:CGFloat = 30.0
Expand Down Expand Up @@ -86,69 +86,64 @@ public class SweetAlert: UIViewController {
self.view.frame.size = mainScreenBounds.size
let x: CGFloat = kWidthMargin
var y: CGFloat = KTopMargin
let width: CGFloat = kContentWidth - (kWidthMargin*2)
let kContentWidth: CGFloat = kMaxWidth - (kWidthMargin*2)

if animatedView != nil {
animatedView!.frame = CGRect(x: (kContentWidth - kAnimatedViewHeight) / 2.0, y: y, width: kAnimatedViewHeight, height: kAnimatedViewHeight)
animatedView!.frame = CGRect(x: (kMaxWidth - kAnimatedViewHeight) / 2.0, y: y, width: kAnimatedViewHeight, height: kAnimatedViewHeight)
contentView.addSubview(animatedView!)
y += kAnimatedViewHeight + kHeightMargin
}

if imageView != nil {
imageView!.frame = CGRect(x: (kContentWidth - kAnimatedViewHeight) / 2.0, y: y, width: kAnimatedViewHeight, height: kAnimatedViewHeight)
imageView!.frame = CGRect(x: (kMaxWidth - kAnimatedViewHeight) / 2.0, y: y, width: kAnimatedViewHeight, height: kAnimatedViewHeight)
contentView.addSubview(imageView!)
y += imageView!.frame.size.height + kHeightMargin

} else if activityIndicatorView != nil {
activityIndicatorView!.frame = CGRect(x: (kContentWidth - kAnimatedViewHeight) / 2.0, y: y, width: kAnimatedViewHeight, height: kAnimatedViewHeight)
activityIndicatorView!.frame = CGRect(x: (kMaxWidth - kAnimatedViewHeight) / 2.0, y: y, width: kAnimatedViewHeight, height: kAnimatedViewHeight)
contentView.addSubview(activityIndicatorView!)
y += activityIndicatorView!.frame.size.height + kHeightMargin
}

// Title
if self.titleLabel.text != nil {
titleLabel.frame = CGRect(x: x, y: y, width: width, height: kTitleHeight)
titleLabel.frame = CGRect(x: x, y: y, width: kContentWidth, height: kTitleHeight)
contentView.addSubview(titleLabel)
y += kTitleHeight + kHeightMargin
}

// Subtitle
if self.subTitleTextView.text.isEmpty == false {
let subtitleString = subTitleTextView.text! as NSString
let rect = subtitleString.boundingRectWithSize(CGSize(width: width, height: 0.0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:subTitleTextView.font!], context: nil)
let rect = subtitleString.boundingRectWithSize(CGSize(width: kContentWidth, height: 0.0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:subTitleTextView.font!], context: nil)
textViewHeight = ceil(rect.size.height) + 10.0
subTitleTextView.frame = CGRect(x: x, y: y, width: width, height: textViewHeight)
subTitleTextView.frame = CGRect(x: x, y: y, width: kContentWidth, height: textViewHeight)
contentView.addSubview(subTitleTextView)
y += textViewHeight + kHeightMargin
}

var buttonRect:[CGRect] = []
for button in buttons {
let string = button.titleForState(UIControlState.Normal)! as NSString
buttonRect.append(string.boundingRectWithSize(CGSize(width: width, height:0.0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes:[NSFontAttributeName:button.titleLabel!.font], context:nil))
}

var totalWidth: CGFloat = 0.0
var buttonWidth: CGFloat = 0.0
if buttons.count == 2 {
totalWidth = buttonRect[0].size.width + buttonRect[1].size.width + kWidthMargin + 40.0
buttonWidth = (kContentWidth / CGFloat(buttons.count)) - kWidthMargin / 2.0
}
else{
totalWidth = 300
buttonWidth = kContentWidth
}

y += kHeightMargin
var buttonX = (kContentWidth - totalWidth ) / 2.0
var buttonX = (kMaxWidth - kContentWidth) / 2.0
for var i = 0; i < buttons.count; i++ {

buttons[i].frame = CGRect(x: buttonX, y: y, width: buttonRect[i].size.width + 20.0, height: buttonRect[i].size.height + 10.0)
buttonX = buttons[i].frame.origin.x + kWidthMargin + buttonRect[i].size.width + 20.0
buttons[i].frame = CGRect(x: buttonX, y: y, width: buttonWidth, height: kButtonHeight)
buttonX = buttonX + buttons[i].bounds.size.width + kWidthMargin
buttons[i].layer.cornerRadius = 5.0
self.contentView.addSubview(buttons[i])
buttons[i].addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)

}

if buttons.count != 0 {
y += kHeightMargin + buttonRect[0].size.height + 10.0
y += kHeightMargin + buttons[0].frame.size.height + 10.0
}

if y > kMaxHeight {
Expand All @@ -164,7 +159,7 @@ public class SweetAlert: UIViewController {
y = kMaxHeight
}

contentView.frame = CGRect(x: (mainScreenBounds.size.width - kContentWidth) / 2.0, y: (mainScreenBounds.size.height - y) / 2.0, width: kContentWidth, height: y)
contentView.frame = CGRect(x: (mainScreenBounds.size.width - kMaxWidth) / 2.0, y: (mainScreenBounds.size.height - y) / 2.0, width: kMaxWidth, height: y)
contentView.clipsToBounds = true
}

Expand Down Expand Up @@ -387,7 +382,7 @@ public class SweetAlert: UIViewController {

class AnimatableView: UIView {
func animate(){
print("Should overide by subclasss", terminator: "")
print("Should overide by subclasss", terminator: " ")
}
}

Expand Down
7 changes: 5 additions & 2 deletions SweetAlert/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ViewController: UIViewController {
SweetAlert().showAlert("Are you sure?", subTitle: "You file will permanently delete!", style: AlertStyle.Warning, dismissTime: nil, buttonTitle:"Cancel", buttonColor:UIColor.colorFromRGB(0xD0D0D0) , otherButtonTitle: "Yes, delete it!", otherButtonColor: UIColor.colorFromRGB(0xDD6B55)) { (isOtherButton) -> Void in
if isOtherButton == true {

print("Cancel Button Pressed", terminator: "")
print("Cancel Button Pressed", terminator: " ")
}
else {
SweetAlert().showAlert("Deleted!", subTitle: "Your imaginary file has been deleted!", style: AlertStyle.Success)
Expand All @@ -68,7 +68,10 @@ class ViewController: UIViewController {
}

@IBAction func customIconAlert(sender: AnyObject) {
SweetAlert().showAlert("Sweet!", subTitle: "Here's a custom image.", style: AlertStyle.CustomImag(imageFile: "thumb.jpg"))
SweetAlert().showAlert("Sweet!", subTitle: "Here's a custom image.", style: AlertStyle.CustomImag(imageFile: "thumb.jpg"), dismissTime: nil, buttonTitle:"Ok", buttonColor:UIColor.colorFromRGB(0xAEDEF4) , otherButtonTitle: nil, otherButtonColor: nil) { (isOtherButton) -> Void in

}

}

}
Expand Down

0 comments on commit dbc29e7

Please sign in to comment.