Ustawia maksymalną długość znaków pola UITextField

Jak mogę ustawić maksymalną ilość znaków w UITextField na iPhone SDK po załadowaniu UIView?

Author: JasonMArcher, 2009-01-11

30 answers

Podczas gdy klasa UITextField nie ma właściwości max length, stosunkowo łatwo jest uzyskać tę funkcjonalność poprzez ustawienie pola tekstowego delegate i zaimplementowanie następującej metody delegata:

Objective-C

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // Prevent crashing undo bug – see note below.
    if(range.length + range.location > textField.text.length)
    {
        return NO;
    }

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return newLength <= 25;
}

Swift

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let currentCharacterCount = textField.text?.characters.count ?? 0
    if (range.length + range.location > currentCharacterCount){
        return false
    }
    let newLength = currentCharacterCount + string.characters.count - range.length
    return newLength <= 25
}

Przed zmianą pola tekstowego pole UITextField pyta delegata, czy podany tekst powinien zostać zmieniony. Pole tekstowe nie zmieniło się w tym momencie, więc pobieramy jego bieżącą długość i ciąg znaków długość, którą wstawiamy (poprzez wklejenie skopiowanego tekstu lub wpisanie pojedynczego znaku za pomocą klawiatury), minus długość zakresu. Jeśli ta wartość jest zbyt długa (więcej niż 25 znaków w tym przykładzie), zwróć NO, aby zabronić zmiany.

Podczas wpisywania pojedynczego znaku na końcu pola tekstowego, range.location będzie długością bieżącego pola, a range.length będzie równa 0, ponieważ niczego nie zastępujemy/nie usuwamy. Wstawianie do środka pola tekstowego oznacza po prostu inny range.location, a wklejenie wielu znaków oznacza, że string ma w sobie więcej niż jeden znak.

Usuwanie pojedynczych znaków lub wycinanie wielu znaków jest określone przez range o niezerowej długości i pustym łańcuchu znaków. Zastąpienie jest tylko usunięciem zakresu z niepustym ciągiem znaków.

Uwaga na błąd "Cofnij"

Jak wspomniano w komentarzach, istnieje błąd z UITextField, który może prowadzić do awarii.

Jeśli wkleisz w pole, ale wklej jest uniemożliwiona przez implementację walidacji operacja wklejania jest nadal rejestrowana w buforze cofania aplikacji. Jeśli następnie uruchomisz undo (potrząsając urządzeniem i potwierdzając Undo), UITextField spróbuje zastąpić łańcuch, który myśli wkleił do siebie pustym łańcuchem. To spowoduje awarię, ponieważ nigdy w rzeczywistości Nie wklejał łańcucha do siebie. Spróbuje zastąpić część łańcucha, która nie istnieje.

Na szczęście można chronić Przed zabiciem się w ten sposób. Musisz tylko upewnić się, że zakres, który proponuje zastąpić , istnieje w jego bieżącym łańcuchu. To jest to, co początkowy sprawdzanie zdrowia psychicznego powyżej robi.

Swift 3.0 z kopiowaniem i wklejaniem działa dobrze.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let str = (textView.text + text)
        if str.characters.count <= 10 {
            return true
        }
        textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10))
        return false
    }
Mam nadzieję, że to ci pomoże.
 979
Author: sickp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-26 06:30:54

Dziękuję august! (Post )

Oto kod, z którym skończyłem, który działa:

#define MAX_LENGTH 20

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0)
    {
        return NO; // return NO to not change text
    }
    else
    {return YES;}
}
 56
Author: Domness,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:54

Swift 4

import UIKit

private var kAssociationKeyMaxLength: Int = 0

extension UITextField {

    @IBInspectable var maxLength: Int {
        get {
            if let length = objc_getAssociatedObject(self, &kAssociationKeyMaxLength) as? Int {
                return length
            } else {
                return Int.max
            }
        }
        set {
            objc_setAssociatedObject(self, &kAssociationKeyMaxLength, newValue, .OBJC_ASSOCIATION_RETAIN)
            addTarget(self, action: #selector(checkMaxLength), for: .editingChanged)
        }
    }

    @objc func checkMaxLength(textField: UITextField) {
        guard let prospectiveText = self.text,
            prospectiveText.count > maxLength
            else {
                return
        }

        let selection = selectedTextRange

        let indexEndOfText = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)
        let substring = prospectiveText[..<indexEndOfText]
        text = String(substring)

        selectedTextRange = selection
    }
}

Edit: Naprawiono problem z wyciekiem pamięci.

Tutaj wpisz opis obrazka

 37
Author: Frouo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-06 14:29:46

Aby uzupełnićAugust odpowiedź, możliwą implementację proponowanej funkcji (Zobacz UITextField ' s delegate).

Nie testowałem domness kodu, ale mój nie utknie, jeśli użytkownik osiągnął limit, i jest kompatybilny z nowym ciągiem, który zastępuje mniejszy lub równy.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //limit the size :
    int limit = 20;
    return !([textField.text length]>limit && [string length] > range.length);
}
 21
Author: Jmini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-07 14:00:31

Nie możesz tego zrobić bezpośrednio - UITextField nie ma atrybutu maxLength , ale możesz ustawić delegata UITextField's, a następnie użyć:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 17
Author: August,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-14 14:37:36

Często masz wiele pól wejściowych o różnej długości.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    int allowedLength;
    switch(textField.tag) {
        case 1: 
            allowedLength = MAXLENGTHNAME;      // triggered for input fields with tag = 1
            break;
        case 2:
            allowedLength = MAXLENGTHADDRESS;   // triggered for input fields with tag = 2
            break;
        default:
            allowedLength = MAXLENGTHDEFAULT;   // length default when no tag (=0) value =255
            break;
    }

    if (textField.text.length >= allowedLength && range.length == 0) {
        return NO; // Change not allowed
    } else {
        return YES; // Change allowed
    }
}
 13
Author: Vincent,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-07 07:39:45

Najlepszym sposobem byłoby ustawienie powiadomienia o zmianie tekstu. W swojej metodzie -awakeFromNib kontrolera widoku będziesz chciał:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextField:) name:@"UITextFieldTextDidChangeNotification" object:myTextField];

Następnie w tej samej klasie dodaj:

- (void)limitTextField:(NSNotification *)note {
    int limit = 20;
    if ([[myTextField stringValue] length] > limit) {
        [myTextField setStringValue:[[myTextField stringValue] substringToIndex:limit]];
    }
}

Następnie połącz outlet myTextField z twoim UITextField i nie pozwoli Ci dodać więcej znaków po osiągnięciu limitu. Pamiętaj, aby dodać to do swojej metody dealloc:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:myTextField];
 12
Author: Martin Pilkington,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-14 14:36:52

I created this UITextFieldLimit subclass:

  • obsługa wielu pól tekstowych
  • Ustaw limit długości tekstu
  • profilaktyka pasty
  • wyświetla etykietę z lewym znakiem wewnątrz pola tekstowego, która zostanie ukryta po zakończeniu edycji.
  • potrząśnij animacją, gdy nie ma żadnych znaków.

Pobierz UITextFieldLimit.h i UITextFieldLimit.m z repozytorium GitHub:

Https://github.com/JonathanGurebo/UITextFieldLimit

I zacząć test!

Oznacz swój utworzony przez storyboard UITextField i połącz go z moją podklasą za pomocą Inspektora tożsamości:

Inspektor Ds. Tożsamości

Następnie możesz połączyć go z Iboutletem i ustawić limit (domyślnie jest to 10).


Twój ViewController.plik h powinien zawierać: (jeśli nie chcesz modyfikować ustawienia, jak limit)

#import "UITextFieldLimit.h"

/.../

@property (weak, nonatomic) IBOutlet UITextFieldLimit *textFieldLimit; // <--Your IBOutlet

Twój ViewController.plik m powinien @synthesize textFieldLimit.


Ustaw limit długości tekstu w kontrolerze ViewController.plik m:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    [textFieldLimit setLimit:25];// <-- and you won't be able to put more than 25 characters in the TextField.
}

Hope the Klasa ci pomaga. Powodzenia!

 11
Author: Jonathan Gurebo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-20 18:25:32

To powinno wystarczyć do rozwiązania problemu(zastąp 4 przez limit, który chcesz). Upewnij się tylko, że dodałeś delegata w IB.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
     NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
     return (newString.length<=4);
}
 11
Author: Nishant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-08 08:39:37

Symuluję rzeczywisty ciąg zastępczy, który ma się zdarzyć, aby obliczyć przyszłą Długość łańcucha:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if([newString length] > maxLength)
       return NO;

    return YES;
}
 7
Author: samvermette,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-28 04:58:48

Wersja Swift 3 // * * * * * To nie zadziała z Swift 2.x! *****//

Najpierw Utwórz nowy plik Swift: TextFieldMaxLength.swift, a następnie dodaj poniższy kod:

import UIKit

private var maxLengths = [UITextField: Int]()

extension UITextField {

   @IBInspectable var maxLength: Int {

      get {

          guard let length = maxLengths[self] 
             else {
                return Int.max
      }
      return length
   }
   set {
      maxLengths[self] = newValue
      addTarget(
         self,
         action: #selector(limitLength),
         for: UIControlEvents.editingChanged
      )
   }
}
func limitLength(textField: UITextField) {
    guard let prospectiveText = textField.text,
        prospectiveText.characters.count > maxLength
    else {
        return
    }

   let selection = selectedTextRange
   let maxCharIndex = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)
   text = prospectiveText.substring(to: maxCharIndex)
   selectedTextRange = selection
   }
}

A następnie zobaczysz w Storyboard nowe pole (Maksymalna Długość) po wybraniu dowolnego pola tekstowego

Jeśli nadal masz więcej pytań sprawdź ten link: http://www.globalnerdy.com/2016/05/18/ios-programming-trick-how-to-use-xcode-to-set-a-text-fields-maximum-length-visual-studio-style/

 7
Author: Gilad Brunfman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-29 15:36:46

Używając Interface builder możesz połączyć się i uzyskać Zdarzenie "Editing changed" W dowolnej funkcji. Teraz możesz sprawdzić długość

- (IBAction)onValueChange:(id)sender 
{
    NSString *text = nil;
    int MAX_LENGTH = 20;
    switch ([sender tag] ) 
    {
        case 1: 
        {
            text = myEditField.text;
            if (MAX_LENGTH < [text length]) {
                myEditField.text = [text substringToIndex:MAX_LENGTH];
            }
        }
            break;
        default:
            break;
    }

}
 6
Author: Vishal Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-16 08:35:52

Poniższy kod jest podobny do odpowiedzi sickpa, ale obsługuje poprawnie operacje kopiowania i wklejania. Jeśli spróbujesz wkleić tekst, który jest dłuższy niż limit, poniższy kod skróci tekst, aby pasował do limitu, zamiast całkowicie odmawiać operacji wklejania.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    static const NSUInteger limit = 70; // we limit to 70 characters
    NSUInteger allowedLength = limit - [textField.text length] + range.length;
    if (string.length > allowedLength) {
        if (string.length > 1) {
            // get at least the part of the new string that fits
            NSString *limitedString = [string substringToIndex:allowedLength];
            NSMutableString *newString = [textField.text mutableCopy];
            [newString replaceCharactersInRange:range withString:limitedString];
            textField.text = newString;
        }
        return NO;
    } else {
        return YES;
    }
}
 6
Author: wroluk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-12 08:02:52

Aby to działało z cut & paste łańcuchów o dowolnej długości, sugerowałbym zmianę funkcji na coś w stylu:

#define MAX_LENGTH 20

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSInteger insertDelta = string.length - range.length;

        if (textField.text.length + insertDelta > MAX_LENGTH)
        {
           return NO; // the new string would be longer than MAX_LENGTH
        }
        else {
            return YES;
        }
    }
 5
Author: Påhl Melin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-13 09:15:19

Jest rozwiązanie ogólne do ustawiania maksymalnej długości w Swift. Przez Ibinspectable można dodać nowy atrybut w Inspektorze atrybutów Xcode.Tutaj wpisz opis obrazka

import UIKit
private var maxLengths = [UITextField: Int]()
extension UITextField {

    @IBInspectable var maxLength: Int {
        get {
            guard let length = maxLengths[self]
            else {
                return Int.max
            }
            return length
        }
        set {
            maxLengths[self] = newValue
            addTarget(
                self,
                action: Selector("limitLength:"),
                forControlEvents: UIControlEvents.EditingChanged
            )
        }
    }

    func limitLength(textField: UITextField) {
        guard let prospectiveText = textField.text
            where prospectiveText.characters.count > maxLength else {
                return
        }
        let selection = selectedTextRange
        text = prospectiveText.substringWithRange(
            Range<String.Index>(prospectiveText.startIndex ..< prospectiveText.startIndex.advancedBy(maxLength))
        )
        selectedTextRange = selection
    }

}
 4
Author: Hiren,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-11 12:17:26

Swift 2.0 +

Najpierw Utwórz klasę dla tego procesu. Nazwijmy to StringValidator.swift.

Następnie po prostu wklej następujący kod wewnątrz niego.

import Foundation

extension String {

func containsCharactersIn(matchCharacters: String) -> Bool {
let characterSet = NSCharacterSet(charactersInString: matchCharacters)
return self.rangeOfCharacterFromSet(characterSet) != nil
}

func containsOnlyCharactersIn(matchCharacters: String) -> Bool {
let disallowedCharacterSet = NSCharacterSet(charactersInString: matchCharacters).invertedSet
return self.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
}


func doesNotContainCharactersIn(matchCharacters: String) -> Bool {
let characterSet = NSCharacterSet(charactersInString: matchCharacters)
return self.rangeOfCharacterFromSet(characterSet) == nil
}

func isNumeric() -> Bool
{
let scanner = NSScanner(string: self)
scanner.locale = NSLocale.currentLocale()

return scanner.scanDecimal(nil) && scanner.atEnd
}

}

Teraz uratuj klasę.....

Użycie..

Teraz masz swój kontroler widoku.Klasa swift i spraw, aby twój textfield był jak..

@IBOutlet weak var contactEntryTxtFld: UITextField! //First textfield
@IBOutlet weak var contactEntryTxtFld2: UITextField!   //Second textfield

Teraz należy użyć metody shouldChangeCharactersInRange textfield i użyć jej w następujący sposób.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if string.characters.count == 0 {
        return true
    }
    let latestText = textField.text ?? ""
    let checkAbleText = (latestText as NSString).stringByReplacingCharactersInRange(range, withString: string)


    switch textField {

    case contactEntryTxtFld:
        return checkAbleText.containsOnlyCharactersIn("0123456789") && prospectiveText.characters.count <= 5

    case contactEntryTxtFld2:
        return checkAbleText.containsOnlyCharactersIn("0123456789") && prospectiveText.characters.count <= 5

    default:
        return true
    }

}

Nie zapomnij ustawić deleguj protokół / metody pól tekstowych.

Pozwól, że ci wyjaśnię... Używam prostego procesu rozszerzenia string, który napisałem wewnątrz innej klasy. Teraz po prostu wywołuję te metody rozszerzenia z innej klasy, gdzie ich potrzebuję, dodając check i maximum value.

Funkcje...

  1. ustawia maksymalne ograniczenie określonego pola tekstowego.
  2. ustawia typ akceptowanych kluczy dla poszczególnych textfield

Typy...

ContainsOnlyCharactersIn / / akceptuje tylko znaki.

ContainsCharactersIn / / akceptuje kombinację znaków

DoesNotContainsCharactersIn / / Will not accept characters

Mam nadzieję, że to pomogło.... Dzięki..
 3
Author: onCompletion,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-31 12:24:14

Swift 3.0

Ten kod działa dobrze, gdy wklejasz ciąg znaków więcej niż Twoje limity znaków.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let str = (textView.text + text)
        if str.characters.count <= 10 {
            return true
        }
        textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10))
        return false
    }
Dziękuję za głosy. :)
 3
Author: ilesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-29 13:35:44

Jest to prawidłowy sposób obsługi maksymalnej długości na UITextField, pozwala on klawisz return na wyjście z pole respondera i pozwala użytkownikowi na backspace, gdy osiągnie limit

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int MAX_LENGHT = 5;
    if([string isEqualToString:@"\n"])
    {
        [textField resignFirstResponder];
        return FALSE;
    }
    else if(textField.text.length > MAX_LENGHT-1)
    {
        if([string isEqualToString:@""] && range.length == 1)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return TRUE;
    }
}
 2
Author: anders,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-23 15:30:17

Dla Xamarin:

YourTextField.ShouldChangeCharacters = 
delegate(UITextField textField, NSRange range, string replacementString)
        {
            return (range.Location + replacementString.Length) <= 4; // MaxLength == 4
        };
 2
Author: Kedu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-01 13:41:09

Swift 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let text = textField.text else { return true }
    let newLength = text.count + string.count - range.length
    return newLength <= 10
}
 2
Author: Shan Ye,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-09 17:07:15

Inne odpowiedzi nie obsługują przypadku, w którym użytkownik może wkleić długi ciąg ze schowka. Jeśli wkleję długi sznurek, powinien być tylko obcięty, ale pokazany. Użyj tego w swoim delegacie:

static const NSUInteger maxNoOfCharacters = 5;

-(IBAction)textdidChange:(UITextField * )textField
{
NSString * text = textField.text;

if(text.length > maxNoOfCharacters)
{
    text = [text substringWithRange:NSMakeRange(0, maxNoOfCharacters)];
    textField.text = text;
}

// use 'text'

}
 1
Author: 8suhas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-04 05:39:20

Mam to do 1 linijki kodu:)

Ustaw delegata widoku tekstowego na "self", a następnie dodaj <UITextViewDelegate> do swojego.h I poniższy kod w Twoim .m .... możesz dostosować liczbę "7", aby była dowolną maksymalną liczbą znaków.

-(BOOL)textView:(UITextView *)a shouldChangeTextInRange:(NSRange)b replacementText:(NSString *)c {
    return ((a.text.length+c.length<=7)+(c.length<1)+(b.length>=c.length)>0);
}

Ten kod służy do wpisywania nowych znaków, usuwania znaków, wybierania znaków, a następnie wpisywania lub usuwania, wybierania znaków i cięcia, wklejania w ogóle, wybierania znaków i wklejanie.

Zrobione!






Alternatywnie, innym fajnym sposobem napisania tego kodu za pomocą operacji bitowych byłoby

-(BOOL)textView:(UITextView *)a shouldChangeTextInRange:(NSRange)b replacementText:(NSString *)c {
    return 0^((a.text.length+c.length<=7)+(c.length<1)+(b.length>=c.length));
}
 1
Author: Albert Renshaw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-13 06:22:41

Mam otwartą podklasę UITextField, STATextField , która oferuje tę funkcjonalność (i wiele więcej) ze swoją właściwością maxCharacterLength.

 1
Author: Stunner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-17 01:02:34

Teraz ile znaków chcesz po prostu dać wartości

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string {
     NSUInteger newLength = [textField.text length] + [string length] - range.length;
     return (newLength > 25) ? NO : YES;
  }
 1
Author: mahesh chowdary,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-08 08:40:04

Użyj tego kodu tutaj RESTRICTED_LENGTH jest długością, którą chcesz ograniczyć dla textfield.

   - (BOOL)textField:(UITextField *)textField     shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == nameTF) {
    int limit = RESTRICTED_LENGTH - 1;
    return !([textField.text length]>limit && [string length] > range.length);
    }
   else
   {
    return YES;
   }

return NO;

}
 1
Author: Amit Shelgaonkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-18 17:43:48

Zrobiłem to w Swift dla limitu 8 znaków podczas korzystania z klawiatury numerycznej.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    return !(textField.text?.characters.count == MAX_LENGTH && string != "")
}

Musiałem przetestować na sznurku != ""aby przycisk delete działał na klawiaturze numerycznej, w przeciwnym razie nie pozwalałby na usuwanie znaków w polu tekstowym po osiągnięciu maksymalnej wartości.

 1
Author: u84six,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-20 20:06:40

Zaimplementowałem rozszerzenie UITextField, aby dodać do niego właściwość maxLength.

Jest oparty na Xcode 6 IBInspectables, więc można ustawić limit maxLength w Konstruktorze interfejsu.

Oto implementacja:

UITextField+MaxLength.h

#import <UIKit/UIKit.h>

@interface UITextField_MaxLength : UITextField<UITextFieldDelegate>

@property (nonatomic)IBInspectable int textMaxLength;
@end

UITextField+MaxLength.m

#import "UITextField+MaxLength.h"

@interface UITextField_MaxLength()

@property (nonatomic, assign) id <UITextFieldDelegate> superDelegate;

@end

@implementation UITextField_MaxLength

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    //validate the length, only if it's set to a non zero value
    if (self.textMaxLength>0) {
        if(range.length + range.location > textField.text.length)
            return NO;

        if (textField.text.length+string.length - range.length>self.textMaxLength) {
            return NO;
        }
    }

    //if length validation was passed, query the super class to see if the delegate method is implemented there
    if (self.superDelegate && [self.superDelegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [self.superDelegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    else{
        //if the super class does not implement the delegate method, simply return YES as the length validation was passed
        return YES;
    }
}

- (void)setDelegate:(id<UITextFieldDelegate>)delegate {
    if (delegate == self)
        return;
    self.superDelegate = delegate;
    [super setDelegate:self];
}

//forward all non overriden delegate methods
- (id)forwardingTargetForSelector:(SEL)aSelector {
    if ([self.superDelegate  respondsToSelector:aSelector])
        return self.superDelegate;

    return [super forwardingTargetForSelector:aSelector];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    if ([self.superDelegate respondsToSelector:aSelector])
        return YES;

    return [super respondsToSelector:aSelector];
}
@end
 1
Author: Lefteris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-05 21:56:45

Udzielam dodatkowej odpowiedzi na podstawie @Frouo. Myślę, że jego odpowiedź jest najpiękniejsza. Ponieważ to wspólna kontrola, którą możemy użyć ponownie.

private var kAssociationKeyMaxLength: Int = 0

extension UITextField {

    @IBInspectable var maxLength: Int {
        get {
            if let length = objc_getAssociatedObject(self, &kAssociationKeyMaxLength) as? Int {
                return length
            } else {
                return Int.max
            }
        }
        set {
            objc_setAssociatedObject(self, &kAssociationKeyMaxLength, newValue, .OBJC_ASSOCIATION_RETAIN)
            self.addTarget(self, action: #selector(checkMaxLength), for: .editingChanged)
        }
    }

    func checkMaxLength(textField: UITextField) {

        guard !self.isInputMethod(), let prospectiveText = self.text,
            prospectiveText.count > maxLength
            else {
                return
        }

        let selection = selectedTextRange
        let maxCharIndex = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)
        text = prospectiveText.substring(to: maxCharIndex)
        selectedTextRange = selection
    }

    //The method is used to cancel the check when use Chinese Pinyin input method.
    //Becuase the alphabet also appears in the textfield when inputting, we should cancel the check.
    func isInputMethod() -> Bool {
        if let positionRange = self.markedTextRange {
            if let _ = self.position(from: positionRange.start, offset: 0) {
                return true
            }
        }
        return false
    }

}
 1
Author: Victor Choy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-06 12:51:21

Problem z niektórymi odpowiedziami podanymi powyżej polega na tym, że na przykład mam pole tekstowe i muszę ustawić limit 15 znaków, po czym przestaje po wpisaniu 15 znaków. ale nie pozwalają usunąć. To jest przycisk Usuń również nie działa. Miałem ten sam problem. Wyszedł z rozwiązaniem, podane poniżej. Działa idealnie dla mnie

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 if(textField.tag==6)
 {
    if ([textField.text length]<=30)
    {
        return YES;   
    }
    else if([@"" isEqualToString:string])
    {
        textField.text=[textField.text substringToIndex:30 ];
    }

    return NO;
 }
 else
 {
    return YES;
 }
}

Mam pole tekstowe, którego znacznik ustawiłem " 6" i ograniczyłem max char limit = 30 ; działa dobrze w każdym case

 0
Author: Jasmeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-21 14:36:40
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if ([txt_name.text length]>100)
    {
        return NO;
    }

    return YES;
}
 0
Author: Mubin Shaikh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-19 14:48:47