neoneye/SwiftyFORM

How to use style class?

Closed this issue · 5 comments

How can I use style class to align titleLabel to left?

I'm sorry Sreejith, there is currently no way to control alignment.

I don't understand you. How would you like it to work?

For ButtonFormItem, the title is aligned to centre. For some cases, I need to align it left.

You can use the CustomFormItem class for a left-aligned UILabel or UIButton.

In the Example project, take a look at the CustomViewController loading the LoadingCell.

Good luck Sreejith

Thank you for feedback on ButtonFormItem.

Ok. I will try. Thanks for your prompt reply. :)

Thanks Simon. It worked!

In case if it helps someone:

  1. Create CustomButtonCell.swift
   import SwiftyFORM

class CustomButtonCell: UITableViewCell, SelectRowDelegate {
    var action: (()->())!
    public init(_ title: String) {
        super.init(style: .default, reuseIdentifier: nil)
        loadWithModel(title: title)
    }
    
    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func loadWithModel(title: String) {
        textLabel?.text = title
        textLabel?.textAlignment = NSTextAlignment.left
    }
    
    public func form_didSelectRow(indexPath: IndexPath, tableView: UITableView) {
        // hide keyboard when the user taps this kind of row
        tableView.form_firstResponder()?.resignFirstResponder()
        action()
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
  1. In populate function of your viewController add this:
let customButton = CustomFormItem()
        customButton.createCell = { _ in
            let customButtonCell: CustomButtonCell = CustomButtonCell("Contact us")
            customButtonCell.action = {
                //Your custom action
            }
            return customButtonCell
        }
builder += customButton