9 Notes for me in May Swift 4.2

1. Capslock UITexfield Editing changed

- Texfield.autocapitalizationType = .allCharacters
- Texfield.autocapitalizationType = .words // first character
- Texfield.autocapitalizationType = .sentences // first character in line
- Texfield.autocapitalizationType = .none

2. Dynamic tableView in scroll view


// public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//    haightConstant.constant = haightConstant.constant + tableView.contentSize.height - tableView.layer.frame.size.height
//    return 10
//}

note:
- haightConstant -> height view in scrollview
- haightConstant.constant -> present height view
-  tableView.contentSize.height -> height per cell
- tableView.layer.frame.size.height -> present height tableview


3. HTML To String Swift 4.2

extension String{
    func convertHtml() -> NSAttributedString{
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do{
            return  try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
        }catch{
            return NSAttributedString()
        }
    }
}

how to use:

result.attributedText = htmlStrng.text?.convertHtml()
-OR-
result.attributedText = "how to use:<br /><br />result.attributedText = htmlStrng.text?.convertHtml()<br />-OR-<br />result.attributedText = "".convertHtml()<br /><br /><br />4.?????<br />.convertHtml()


4. Image URL to ImageView without Library


        do {
            let url = URL(string: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg")
            let data = try Data(contentsOf: url!)
            self.imageView.image = UIImage(data: data)
        }
        catch {
            print(error)
        }


Or use an extension

extension UIImageView {
    func load(url: URL) {
        DispatchQueue.global().async { [weak self] in
            if let data = try? Data(contentsOf: url) {
                if let image = UIImage(data: data) {
                    DispatchQueue.main.async {
                        self?.image = image
                    }
                }
            }
        }
    }
}



5. Need Optional in URL 

Example if you found error like this :



Try this solution use if  before let variable


if let url = URL(string: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg") {
            imageView.load(url: url)
        }


6. Background for Stackview

extension UIStackView {

    func addBackground(color: UIColor) {

        let subView = UIView(frame: bounds)

        subView.backgroundColor = color

        subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        insertSubview(subView, at: 0)

    }

}

how to use:

// stackview. addBackground(color: UIColor.blue)


7. Multiple Looping Array 

@IBOutlet var titleChild: [UILabel]!  // outlet collection

let dataArrayString: [String] = ["a", "b", "c"]
for (title, data) in zip(titleChild, dataArrayString) { title.text = data.uppercased() }



8. Set Maximum character in textfield

I use this step, first Set delegate texfield in viewdidload.
    override func viewDidLoad() {
        super.viewDidLoad()

        textfield.delegate = self

    }
and then shouldChangeCharactersIn after you include UITextFieldDelegate.
extension viewController: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
                let newLength = (textField.text?.utf16.count)! + string.utf16.count - range.length
                if newLength <= 8 { 
                    return true
                } else {
                    return false
                }
            }
    }

9. Regex for phone number 

example: Japan extension country (+81)

   func isPhoneNumbersPrefix() -> String {

        let pattern = "^((?:\\+81|81)|0|[1-9]{0})"

        do {

            let regex = try NSRegularExpression(pattern: pattern, options: [])

            let range = NSRange(location: 0, length: self.count)

            let newString = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "0")

            return newString

        } catch {

            NSLog("replaceAll error: \(error)")

        }

        return self

    }



No comments: