Skip to content

Instantly share code, notes, and snippets.

@willard1218
Created April 2, 2018 03:24
Show Gist options
  • Save willard1218/88149ca5e56d06be54e37c5e1730c9e7 to your computer and use it in GitHub Desktop.
Save willard1218/88149ca5e56d06be54e37c5e1730c9e7 to your computer and use it in GitHub Desktop.
eee
//
// ViewController.swift
// text
//
// Created by willard on 2018/4/2.
// Copyright © 2018年 willard. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textfield.addTarget(self, action: #selector(textDidChanged), for: .editingChanged)
}
@objc
func textDidChanged() {
if let text = textfield.text {
if text.count > 5 {
let start = text.index(text.startIndex, offsetBy: 0)
let end = text.index(text.startIndex, offsetBy: 5)
textfield.text = String(text[start..<end])
}
}
label.text = textfield.text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment