Created
April 2, 2018 03:24
-
-
Save willard1218/88149ca5e56d06be54e37c5e1730c9e7 to your computer and use it in GitHub Desktop.
eee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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