Stripping newlines/blanks when pasting on an NSTextField

  • Hi, i have a problem with an NSTextField. I would like it to behave
    like a real single-line field, that is, the sure should be unable to
    insert newlines even when copying and pasting to it, and should have
    immediate visual feedback of what the string will be like. Would
    using a custom NSFormatter make sense in that case? Any advice? Thank
    you very much for your help.

    Regards,
    Michele Balistreri
    brik. Lead programmer.
  • Try performing stringByTrimmingCharactersInSet:[NSCharacterSet
    whitespaceAndNewlineCharacterSet] on the string.

    Ack, at 10/28/06, Michele Balistreri said:

    > Hi, i have a problem with an NSTextField. I would like it to behave
    > like a real single-line field, that is, the sure should be unable to
    > insert newlines even when copying and pasting to it, and should have
    > immediate visual feedback of what the string will be like. Would
    > using a custom NSFormatter make sense in that case? Any advice?
    > Thank you very much for your help.

    --

    Sincerely,
    Rosyna Keller
    Technical Support/Carbon troll/Always needs a hug

    Unsanity: Unsane Tools for Insanely Great People

    It's either this, or imagining Phil Schiller in a thong.
  • On Oct 28, 2006, at 2:50 PM, Michele Balistreri wrote:

    > Hi, i have a problem with an NSTextField. I would like it to behave
    > like a real single-line field, that is, the sure should be unable
    > to insert newlines even when copying and pasting to it, and should
    > have immediate visual feedback of what the string will be like.
    > Would using a custom NSFormatter make sense in that case? Any
    > advice? Thank you very much for your help.

    Yes, a formatter would be a good solution.

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • On Oct 28, 2006, at 5:05 PM, Rosyna wrote:
    > Try performing stringByTrimmingCharactersInSet:[NSCharacterSet
    > whitespaceAndNewlineCharacterSet] on the string.

      +whitespaceAndNewlineCharacterSet removes spaces too. ;-)

    --
    I.S.
  • Only at the ends.

    "
    Bob's your uncle. "

    Becomes:

    "Bob's your uncle."

    That's what the "trim" is doing.

    Ack, at 10/28/06, I. Savant said:

    > On Oct 28, 2006, at 5:05 PM, Rosyna wrote:
    >> Try performing stringByTrimmingCharactersInSet:[NSCharacterSet
    >> whitespaceAndNewlineCharacterSet] on the string.
    >
    > +whitespaceAndNewlineCharacterSet removes spaces too. ;-)

    --

    Sincerely,
    Rosyna Keller
    Technical Support/Carbon troll/Always needs a hug

    Unsanity: Unsane Tools for Insanely Great People

    It's either this, or imagining Phil Schiller in a thong.
  • Ah, you're absolutely right - my eyes were drawn directly to the
    character set and I failed to see the forest for the trees.

      I'm still seeing a problem, though: You're trimming only the ends,
    but what about the middle? What if the user copies a few paragraphs
    from a text editor and pastes it here?

    --
    I.S.

    On Oct 28, 2006, at 5:34 PM, Rosyna wrote:

    > Only at the ends.
    >
    > "
    > Bob's your uncle. "
    >
    > Becomes:
    >
    > "Bob's your uncle."
    >
    > That's what the "trim" is doing.
    >
    > Ack, at 10/28/06, I. Savant said:
    >
    >> On Oct 28, 2006, at 5:05 PM, Rosyna wrote:
    >>> Try performing stringByTrimmingCharactersInSet:[NSCharacterSet
    >>> whitespaceAndNewlineCharacterSet] on the string.
    >>
    >> +whitespaceAndNewlineCharacterSet removes spaces too. ;-)
    >
    > --
    >
    >
    > Sincerely,
    > Rosyna Keller
    > Technical Support/Carbon troll/Always needs a hug
    >
    > Unsanity: Unsane Tools for Insanely Great People
    >
    > It's either this, or imagining Phil Schiller in a thong.
  • Most of the time (in my experience) users only have leading or
    trailing whitespace that needs to be remove when pasting into text
    fields due to Cocoa's silly text selection "rules".

    Since, in our example, they're pasting in codes from emails, all we
    worry about is the trailing and leading whitespace. Of course, YMMV.

    Ack, at 10/28/06, I. Savant said:

    >
    > I'm still seeing a problem, though: You're trimming only the ends,
    > but what about the middle? What if the user copies a few paragraphs
    > from a text editor and pastes it here?

    --

    Sincerely,
    Rosyna Keller
    Technical Support/Carbon troll/Always needs a hug

    Unsanity: Unsane Tools for Insanely Great People

    It's either this, or imagining Phil Schiller in a thong.
  • Hi, thank you for your help, i know how to remove characters from a
    string, my problem was giving them instant feedback of what has been
    done, say they paste 2 lines, they should be directly displayed as
    one in the field. That is possible only with formatters, or there are
    some special options of NSTextField?

    Regards,
    Michele Balistreri

    Il giorno 28/ott/06, alle ore 23:51, Rosyna ha scritto:

    > Most of the time (in my experience) users only have leading or
    > trailing whitespace that needs to be remove when pasting into text
    > fields due to Cocoa's silly text selection "rules".
    >
    > Since, in our example, they're pasting in codes from emails, all we
    > worry about is the trailing and leading whitespace. Of course, YMMV.
    >
    > Ack, at 10/28/06, I. Savant said:
    >
    >>
    >> I'm still seeing a problem, though: You're trimming only the
    >> ends, but what about the middle? What if the user copies a few
    >> paragraphs from a text editor and pastes it here?
    >
    > --
    >
    >
    > Sincerely,
    > Rosyna Keller
    > Technical Support/Carbon troll/Always needs a hug
    >
    > Unsanity: Unsane Tools for Insanely Great People
    >
    > It's either this, or imagining Phil Schiller in a thong.
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/mbalistreri%
    > 40briksoftware.com
    >
    > This email sent to <mbalistreri...>

    Regards,
    Michele Balistreri
    brik. Lead programmer.
  • On Oct 28, 2006, at 6:05 PM, Michele Balistreri wrote:
    > Hi, thank you for your help, i know how to remove characters from a
    > string, my problem was giving them instant feedback of what has
    > been done, say they paste 2 lines, they should be directly
    > displayed as one in the field. That is possible only with
    > formatters, or there are some special options of NSTextField?

      Since you said removing characters from a string isn't the
    problem, I'll leave that part alone and address the 'immediate
    formatting' part. Sorry for the confusion. ;-)

      Just having a filtering formatter isn't enough since the input
    isn't formatted until editing is done. A simple approach would be to
    use the -controlTextDidChange: delegate method to force the
    formatting by calling -validateEditing on the text field in question
    when the text changes:

    - (void)controlTextDidChange:(NSNotification *)aNotification
    {
        [[aNotification object] validateEditing];
    }

      This wouldn't work with NSNumberFormatter or NSDateFormatter since
    rejected input returns an empty string, so nothing appears to have
    happened but a system beep. Our filtering formatter will always
    return a string, so that shouldn't be a problem here.

    --
    I.S.
  • Thank you for your help. In the end i just used a formatter also
    implementing isPartialString.. to be called at each keypress

    - (NSString *)stringForObjectValue:(id)anObject
    {
    return [[(NSString *)anObject componentsSeparatedByString:@"\n"]
    componentsJoinedByString:@""];

    }

    - (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string
    errorDescription:(NSString **)error
    {
    *anObject = [[(NSString *)string componentsSeparatedByString:@"\n"]
    componentsJoinedByString:@""];
    return YES;
    }

    - (BOOL)isPartialStringValid:(NSString *)partialString
    newEditingString:(NSString **)newString errorDescription:(NSString **)
    error
    {
    *newString = [[(NSString *)partialString
    componentsSeparatedByString:@"\n"] componentsJoinedByString:@""];
    return NO;
    }

    This is the code, maybe not the best way to strip white characters,
    but trimming is not enough for me unfortunately. Indeed if someone
    knows a better way (maybe a little more flexible), please advice.

    Regards,
    Michele Balistreri
    brik. Lead programmer.

    Il giorno 29/ott/06, alle ore 00:51, I. Savant ha scritto:

    > On Oct 28, 2006, at 6:05 PM, Michele Balistreri wrote:
    >> Hi, thank you for your help, i know how to remove characters from
    >> a string, my problem was giving them instant feedback of what has
    >> been done, say they paste 2 lines, they should be directly
    >> displayed as one in the field. That is possible only with
    >> formatters, or there are some special options of NSTextField?
    >
    > Since you said removing characters from a string isn't the
    > problem, I'll leave that part alone and address the 'immediate
    > formatting' part. Sorry for the confusion. ;-)
    >
    > Just having a filtering formatter isn't enough since the input
    > isn't formatted until editing is done. A simple approach would be
    > to use the -controlTextDidChange: delegate method to force the
    > formatting by calling -validateEditing on the text field in
    > question when the text changes:
    >
    > - (void)controlTextDidChange:(NSNotification *)aNotification
    > {
    > [[aNotification object] validateEditing];
    > }
    >
    > This wouldn't work with NSNumberFormatter or NSDateFormatter
    > since rejected input returns an empty string, so nothing appears to
    > have happened but a system beep. Our filtering formatter will
    > always return a string, so that shouldn't be a problem here.
    >
    > --
    > I.S.
    >
    >
    >
  • On Oct 28, 2006, at 15:59, Michele Balistreri wrote:

    [...]
    >
    > This is the code, maybe not the best way to strip white characters,
    > but trimming is not enough for me unfortunately. Indeed if someone
    > knows a better way (maybe a little more flexible), please advice.

    Hi Michele,

    I'd use a more robust character set definition; here's a category
    method that I've used (modified in Mail, so beware of typos).  With
    that, you can make a mutable copy of your string, then use -[NSString
    rangeOfCharacterFromSet:] and -[NSMutableString
    deleteCharactersInRange:] to remove the newlines (probably in an
    NSString category method).

    hth,
    Adam

    @interface NSCharacterSet (NewlineSet)
    + (NSCharacterSet *)newlineCharacterSet;
    @end

    @implementation NSCharacterSet (NewlineSet)

    + (NSCharacterSet *)newlineCharacterSet;
    {
        static NSCharacterSet *newlineCharacterSet = nil;
        if (nil == newlineCharacterSet) {
          // This will be a character set with all newline characters
    (including the weird Unicode ones)
          CFMutableCharacterSetRef newlineCFCharacterSet = NULL;
          // get all whitespace characters (does not include newlines)
          newlineCFCharacterSet = CFCharacterSetCreateMutableCopy
    (CFAllocatorGetDefault(), CFCharacterSetGetPredefined
    (kCFCharacterSetWhitespace));
          // invert the whitespace-only set to get all non-whitespace
    chars (the inverted set will include newlines)
          CFCharacterSetInvert(newlineCFCharacterSet);
          // now get only the characters that are common to
    kCFCharacterSetWhitespaceAndNewline and our non-whitespace set
          CFCharacterSetIntersect(newlineCFCharacterSet,
    CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline));
        newlineCharacterSet = [(id)newlineCFCharacterSet copy];
        }
        return newlineCharacterSet;
    }
    @end
  • On Oct 28, 2006, at 6:59 PM, Michele Balistreri wrote:
    > Thank you for your help. In the end i just used a formatter also
    > implementing isPartialString.. to be called at each keypress

      Ha! Serves me right for not double-checking the docs before
    answering. In my defense, I haven't needed much beyond a number or
    date formatter in my ventures and the few times I have, it's been a
    "correct or nothing" situation. As a result I had completely
    forgotten about that method. Regardless, insert your local 'no
    excuse' idiom here. ;-)

      As to a more flexible way to filter strings, I would definitely
    use NSScanner's -scanCharactersFromSet:intoString: method to filter a
    string for any character set (including newlines, etc.). You can
    easily create a reusable "MyStringFilteringFormatter" subclass if you
    want, that allows you to set which characters it should filter. This
    way, the actual character set you're filtering doesn't matter so you
    can reuse this formatter anywhere to filter a variety of things. Of
    course it's up to you to supply the character set.

      I hope at least *that* is helpful. :-)

    --
    I.S.
  • Hi,

    Unfortunately, due to bugs with the framework, you'll get an
    exception if you try and undo.

    I tried this a couple of weeks back. I've reported it to Apple.

    If anyone knows any workaround, I'd like to know.

    - Chris

    On 29/10/2006, at 9:59 AM, Michele Balistreri wrote:

    > Thank you for your help. In the end i just used a formatter also
    > implementing isPartialString.. to be called at each keypress
    >
    > - (NSString *)stringForObjectValue:(id)anObject
    > {
    > return [[(NSString *)anObject componentsSeparatedByString:@"\n"]
    > componentsJoinedByString:@""];
    >
    > }
    >
    > - (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string
    > errorDescription:(NSString **)error
    > {
    > *anObject = [[(NSString *)string
    > componentsSeparatedByString:@"\n"] componentsJoinedByString:@""];
    > return YES;
    > }
    >
    > - (BOOL)isPartialStringValid:(NSString *)partialString
    > newEditingString:(NSString **)newString errorDescription:(NSString
    > **)error
    > {
    > *newString = [[(NSString *)partialString
    > componentsSeparatedByString:@"\n"] componentsJoinedByString:@""];
    > return NO;
    > }
    >
    > This is the code, maybe not the best way to strip white characters,
    > but trimming is not enough for me unfortunately. Indeed if someone
    > knows a better way (maybe a little more flexible), please advice.
    >
    > Regards,
    > Michele Balistreri
    > brik. Lead programmer.
    >
    >
    >
    > Il giorno 29/ott/06, alle ore 00:51, I. Savant ha scritto:
    >
    >> On Oct 28, 2006, at 6:05 PM, Michele Balistreri wrote:
    >>> Hi, thank you for your help, i know how to remove characters from
    >>> a string, my problem was giving them instant feedback of what has
    >>> been done, say they paste 2 lines, they should be directly
    >>> displayed as one in the field. That is possible only with
    >>> formatters, or there are some special options of NSTextField?
    >>
    >> Since you said removing characters from a string isn't the
    >> problem, I'll leave that part alone and address the 'immediate
    >> formatting' part. Sorry for the confusion. ;-)
    >>
    >> Just having a filtering formatter isn't enough since the input
    >> isn't formatted until editing is done. A simple approach would be
    >> to use the -controlTextDidChange: delegate method to force the
    >> formatting by calling -validateEditing on the text field in
    >> question when the text changes:
    >>
    >> - (void)controlTextDidChange:(NSNotification *)aNotification
    >> {
    >> [[aNotification object] validateEditing];
    >> }
    >>
    >> This wouldn't work with NSNumberFormatter or NSDateFormatter
    >> since rejected input returns an empty string, so nothing appears
    >> to have happened but a system beep. Our filtering formatter will
    >> always return a string, so that shouldn't be a problem here.
    >>
    >> --
    >> I.S.
    >>
    >>
    >>
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<chris...>-
    > systems.com
    >
    > This email sent to <chris...>
  • Hi, indeed the scanner is a good idea, thank you! :) Also the
    character set definition proposed by Adam is great for my purpose .

    Regards,
    Michele Balistreri
    brik. Lead programmer.

    Il giorno 29/ott/06, alle ore 01:22, I. Savant ha scritto:

    > On Oct 28, 2006, at 6:59 PM, Michele Balistreri wrote:
    >> Thank you for your help. In the end i just used a formatter also
    >> implementing isPartialString.. to be called at each keypress
    >
    > Ha! Serves me right for not double-checking the docs before
    > answering. In my defense, I haven't needed much beyond a number or
    > date formatter in my ventures and the few times I have, it's been a
    > "correct or nothing" situation. As a result I had completely
    > forgotten about that method. Regardless, insert your local 'no
    > excuse' idiom here. ;-)
    >
    > As to a more flexible way to filter strings, I would definitely
    > use NSScanner's -scanCharactersFromSet:intoString: method to filter
    > a string for any character set (including newlines, etc.). You can
    > easily create a reusable "MyStringFilteringFormatter" subclass if
    > you want, that allows you to set which characters it should filter.
    > This way, the actual character set you're filtering doesn't matter
    > so you can reuse this formatter anywhere to filter a variety of
    > things. Of course it's up to you to supply the character set.
    >
    > I hope at least *that* is helpful. :-)
    >
    > --
    > I.S.
    >
    >
    >
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today