Come limitare la lunghezza del testo di NSTextField e mantenerlo sempre in maiuscolo?
-
05-07-2019 - |
Domanda
È necessario disporre di un NSTextField con un limite di testo massimo di 4 caratteri e mostrare sempre in maiuscolo, ma non riesco a capire un buon modo per ottenerlo. Ho provato a farlo attraverso un'associazione con un metodo di convalida, ma la convalida viene chiamata solo quando il controllo perde il primo risponditore e non va bene.
Temporaneamente l'ho fatto funzionare osservando la notifica NSControlTextDidChangeNotification sul campo di testo e facendolo chiamare il metodo:
- (void)textDidChange:(NSNotification*)notification {
NSTextField* textField = [notification object];
NSString* value = [textField stringValue];
if ([value length] > 4) {
[textField setStringValue:[[value uppercaseString] substringWithRange:NSMakeRange(0, 4)]];
} else {
[textField setStringValue:[value uppercaseString]];
}
}
Ma questo sicuramente non è il modo migliore per farlo. Qualche suggerimento migliore?
Soluzione
Ho fatto come suggerito Graham Lee e funziona benissimo, ecco il codice del formatter personalizzato:
AGGIORNATO: aggiunta correzione segnalata da Dave Gallagher. Grazie!
@interface CustomTextFieldFormatter : NSFormatter {
int maxLength;
}
- (void)setMaximumLength:(int)len;
- (int)maximumLength;
@end
@implementation CustomTextFieldFormatter
- (id)init {
if(self = [super init]){
maxLength = INT_MAX;
}
return self;
}
- (void)setMaximumLength:(int)len {
maxLength = len;
}
- (int)maximumLength {
return maxLength;
}
- (NSString *)stringForObjectValue:(id)object {
return (NSString *)object;
}
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
*object = string;
return YES;
}
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error {
if ([*partialStringPtr length] > maxLength) {
return NO;
}
if (![*partialStringPtr isEqual:[*partialStringPtr uppercaseString]]) {
*partialStringPtr = [*partialStringPtr uppercaseString];
return NO;
}
return YES;
}
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes {
return nil;
}
@end
Altri suggerimenti
Nell'esempio sopra in cui ho commentato, questo è male:
// Don't use:
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString
errorDescription:(NSString **)error
{
if ((int)[partialString length] > maxLength)
{
*newString = nil;
return NO;
}
}
Usa questo (o qualcosa del genere) invece:
// Good to use:
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error
{
int size = [*partialStringPtr length];
if ( size > maxLength )
{
return NO;
}
return YES;
}
Entrambi sono metodi NSFormatter. Il primo ha un problema. Supponi di limitare l'inserimento di testo a 10 caratteri. Se digiti i caratteri uno a uno in un NSTextField, funzionerà bene e impedirà agli utenti di superare i 10 caratteri.
Tuttavia, se un utente dovesse incollare una stringa di, diciamo, 25 caratteri nel campo di testo, ciò che accadrà sarà qualcosa del genere:
1) L'utente incollerà in TextField
2) TextField accetterà la stringa di caratteri
3) TextField applicherà il formattatore all'ultimo " " carattere nella stringa di 25 lunghezze
4) Il formatter esegue roba fino all'ultimo " carattere nella stringa di 25 lunghezze, ignorando il resto
5) TextField finirà con 25 caratteri, anche se è limitato a 10.
Questo perché, credo, il primo metodo si applica solo all'ultimo carattere " digitato in un NSTextField. Il secondo metodo mostrato sopra si applica a " tutti i caratteri " digitato in NSTextField. Quindi è immune da " incolla " sfruttare.
L'ho scoperto proprio ora cercando di rompere la mia domanda e non sono un esperto di NSFormatter, quindi per favore correggimi se sbaglio. E grazie a te carlosb per aver pubblicato quell'esempio. Mi ha aiutato MOLTO! :)
Hai provato ad allegare una sottoclasse NSFormatter
personalizzata?
Questa implementazione adotta molti dei suggerimenti commentati sopra. In particolare, funziona correttamente con gli attacchi in continuo aggiornamento.
Inoltre:
-
Implementa incolla correttamente.
-
Include alcune note su come utilizzare la classe in modo efficace in un pennino senza ulteriori sottoclassi.
Il codice:
@interface BPPlainTextFormatter : NSFormatter {
NSInteger _maxLength;
}
/*
Set the maximum string length.
Note that to use this class within a Nib:
1. Add an NSFormatter as a Custom Formatter.
2. In the Identity inspector set the Class to BPPlainTextFormatter
3. In user defined attributes add Key Path: maxLength Type: Number Value: 30
Note that rather than attaching formatter instances to individual cells they
can be positioned in the nib Objects section and referenced by numerous controls.
A name, such as Plain Text Formatter 100, can be used to identify the formatters max length.
*/
@property NSInteger maxLength;
@end
@implementation BPPlainTextFormatter
@synthesize maxLength = _maxLength;
- (id)init
{
if(self = [super init]){
self.maxLength = INT_MAX;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
// support Nib based initialisation
self = [super initWithCoder:aDecoder];
if (self) {
self.maxLength = INT_MAX;
}
return self;
}
#pragma mark -
#pragma mark Textual Representation of Cell Content
- (NSString *)stringForObjectValue:(id)object
{
NSString *stringValue = nil;
if ([object isKindOfClass:[NSString class]]) {
// A new NSString is perhaps not required here
// but generically a new object would be generated
stringValue = [NSString stringWithString:object];
}
return stringValue;
}
#pragma mark -
#pragma mark Object Equivalent to Textual Representation
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error
{
BOOL valid = YES;
// Be sure to generate a new object here or binding woe ensues
// when continuously updating bindings are enabled.
*object = [NSString stringWithString:string];
return valid;
}
#pragma mark -
#pragma mark Dynamic Cell Editing
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error
{
BOOL valid = YES;
NSString *proposedString = *partialStringPtr;
if ([proposedString length] > self.maxLength) {
// The original string has been modified by one or more characters (via pasting).
// Either way compute how much of the proposed string can be accommodated.
NSInteger origLength = origString.length;
NSInteger insertLength = self.maxLength - origLength;
// If a range is selected then characters in that range will be removed
// so adjust the insert length accordingly
insertLength += origSelRange.length;
// Get the string components
NSString *prefix = [origString substringToIndex:origSelRange.location];
NSString *suffix = [origString substringFromIndex:origSelRange.location + origSelRange.length];
NSString *insert = [proposedString substringWithRange:NSMakeRange(origSelRange.location, insertLength)];
#ifdef _TRACE
NSLog(@"Original string: %@", origString);
NSLog(@"Original selection location: %u length %u", origSelRange.location, origSelRange.length);
NSLog(@"Proposed string: %@", proposedString);
NSLog(@"Proposed selection location: %u length %u", proposedSelRangePtr->location, proposedSelRangePtr->length);
NSLog(@"Prefix: %@", prefix);
NSLog(@"Suffix: %@", suffix);
NSLog(@"Insert: %@", insert);
#endif
// Assemble the final string
*partialStringPtr = [[NSString stringWithFormat:@"%@%@%@", prefix, insert, suffix] uppercaseString];
// Fix-up the proposed selection range
proposedSelRangePtr->location = origSelRange.location + insertLength;
proposedSelRangePtr->length = 0;
#ifdef _TRACE
NSLog(@"Final string: %@", *partialStringPtr);
NSLog(@"Final selection location: %u length %u", proposedSelRangePtr->location, proposedSelRangePtr->length);
#endif
valid = NO;
}
return valid;
}
@end
Avevo bisogno di un Formatter da convertire in maiuscolo per Swift 4. Per riferimento l'ho incluso qui:
import Foundation
class UppercaseFormatter : Formatter {
override func string(for obj: Any?) -> String? {
if let stringValue = obj as? String {
return stringValue.uppercased()
}
return nil
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
obj?.pointee = string as AnyObject
return true
}
}
Il NSFormatter personalizzato suggerito da Graham Lee è l'approccio migliore.
Un semplice kludge sarebbe impostare il controller della vista come delegato del campo di testo, quindi bloccare qualsiasi modifica che implichi caratteri maiuscoli o rendere la lunghezza superiore a 4:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSMutableString *newValue = [[textField.text mutableCopy] autorelease];
[newValue replaceCharactersInRange:range withString:string];
NSCharacterSet *nonUppercase =
[[NSCharacterSet uppercaseLetterCharacterSet] invertedSet];
if ([newValue length] > 4 ||
[newValue rangeOfCharacterFromSet:nonUppercase].location !=
NSNotFound)
{
return NO;
}
return YES;
}