Skip to content

Commit

Permalink
Merge pull request #79 from frantzmiccoli/expandable-textview
Browse files Browse the repository at this point in the history
Now the view containing the subtitle expands according to its content
  • Loading branch information
vikmeup committed Jul 16, 2015
2 parents 007a53a + aa71236 commit 4e272ce
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions SCLAlertView/SCLAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class SCLAlertView: UIViewController {
let kWindowWidth: CGFloat = 240.0
var kWindowHeight: CGFloat = 178.0
var kTextHeight: CGFloat = 90.0
let kTextFieldHeight: CGFloat = 45.0
let kButtonHeight: CGFloat = 45.0

// Font
let kDefaultFont = "HelveticaNeue"
Expand Down Expand Up @@ -170,28 +172,50 @@ public class SCLAlertView: UIViewController {
}
// Set background frame
view.frame.size = sz

// computing the right size to use for the textView
let maxHeight = sz.height - 100 // max overall height
var consumedHeight = CGFloat(0)
consumedHeight += kTitleTop + kTitleHeight
consumedHeight += 14
consumedHeight += kButtonHeight * CGFloat(buttons.count)
consumedHeight += kTextFieldHeight * CGFloat(inputs.count)
let maxViewTextHeight = maxHeight - consumedHeight
let viewTextWidth = kWindowWidth - 24
let suggestedViewTextSize = viewText.sizeThatFits(CGSizeMake(viewTextWidth, CGFloat.max))
let viewTextHeight = min(suggestedViewTextSize.height, maxViewTextHeight)

// scroll management
if (suggestedViewTextSize.height > maxViewTextHeight) {
viewText.scrollEnabled = true
} else {
viewText.scrollEnabled = false
}

let windowHeight = consumedHeight + viewTextHeight
// Set frames
var x = (sz.width - kWindowWidth) / 2
var y = (sz.height - kWindowHeight - (kCircleHeight / 8)) / 2
contentView.frame = CGRect(x:x, y:y, width:kWindowWidth, height:kWindowHeight)
var y = (sz.height - windowHeight - (kCircleHeight / 8)) / 2
contentView.frame = CGRect(x:x, y:y, width:kWindowWidth, height:windowHeight)
y -= kCircleHeightBackground * 0.6
x = (sz.width - kCircleHeightBackground) / 2
circleBG.frame = CGRect(x:x, y:y+6, width:kCircleHeightBackground, height:kCircleHeightBackground)
// Subtitle
y = kTitleTop + kTitleHeight
viewText.frame = CGRect(x:12, y:y, width: kWindowWidth - 24, height:kTextHeight)
viewText.frame = CGRect(x:12, y:y, width: viewTextWidth, height:viewTextHeight)
// Text fields
y += kTextHeight + 14.0
y += viewTextHeight + 14.0
for txt in inputs {
txt.frame = CGRect(x:12, y:y, width:kWindowWidth - 24, height:30)
txt.layer.cornerRadius = 3
y += 40
y += kTextFieldHeight
}
// Buttons
for btn in buttons {
btn.frame = CGRect(x:12, y:y, width:kWindowWidth - 24, height:35)
btn.layer.cornerRadius = 3
y += 45.0
y += kButtonHeight
}
}

Expand All @@ -203,7 +227,7 @@ public class SCLAlertView: UIViewController {

public func addTextField(title:String?=nil)->UITextField {
// Update view height
kWindowHeight += 40.0
kWindowHeight += kTextFieldHeight
// Add text field
let txt = UITextField()
txt.borderStyle = UITextBorderStyle.RoundedRect
Expand Down Expand Up @@ -243,7 +267,7 @@ public class SCLAlertView: UIViewController {

private func addButton(title:String)->SCLButton {
// Update view height
kWindowHeight += 45.0
kWindowHeight += kButtonHeight
// Add button
let btn = SCLButton()
btn.layer.masksToBounds = true
Expand Down

0 comments on commit 4e272ce

Please sign in to comment.