NSRange range; range = NSMakeRange ([[textView string] length], 0); [textView replaceCharactersInRange: range withString: string];And Peter Hosey provided a one-liner that may suit your needs better:
[[[textView textStorage] mutableString] appendString: string];
[textView delete: nil]
- (NSParagraphStyle *) paragraphStyleInTextView: (NSTextView *) textView atIndex: (int) index { NSTextStorage *storage = [textView textStorage]; NSDictionary *attributes; NSRange effectiveRange; attributes = [storage attributesAtIndex: index effectiveRange: &effectiveRange]; NSParagraphStyle *style; style = [attributes valueForKey: NSParagraphStyleAttributeName]; return (style); } // paragraphStyleInTextView
NSRange selectedRange = [textview selectedRange]; NSTextStorage *storage = [textview textStorage]; effectiveRange = [[storage string] paragraphRangeForRange: selectedRange];
[window makeFirstResponder: window]
NSRange range = { [[textView string] length], 0 };
[textView setSelectedRange: range];
NSRange zeroRange = { 0, 0 };
[textView setSelectedRange: zeroRange];
[[textView textStorage] setAttributedString: theNewString];
NSNumberFormatter
, add this method, and hook up the formatter to your text fields.
- (BOOL) isPartialStringValid: (NSString **) partialStringPtr proposedSelectedRange: (NSRangePointer) proposedSelRangePtr originalString: (NSString *) origString originalSelectedRange: (NSRange) origSelRange errorDescription: (NSString **) error { NSCharacterSet *nonDigits; NSRange newStuff; NSString *newStuffString; nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; newStuff = NSMakeRange(origSelRange.location, proposedSelRangePtr->location - origSelRange.location); newStuffString = [*partialStringPtr substringWithRange: newStuff]; if ([newStuffString rangeOfCharacterFromSet: nonDigits options: NSLiteralSearch].location != NSNotFound) { *error = @"Input is not an integer"; return (NO); } else { *error = nil; return (YES); } } // isPartialStringValid
NSRange range; range = NSMakeRange ([[textView string] length], 0); [textView scrollRangeToVisible: range];I've heard that
scrollRangeToVisible
is O(N) for the length of the text. So be on the lookout if you have lots of text involved.disabledControlTextColor
won't draw the text in the disabled color. You need to use the secondarySelectedControlColor
, which supposedly is for active controls that don't have focus. Go figure.
To do it yourself, subclass NSTextField and override setEnabled:
to change the color:
- (void) setEnabled: (BOOL) flag { [super setEnabled: flag]; if (flag == NO) { [self setTextColor: [NSColor secondarySelectedControlColor]]; } else { [self setTextColor: [NSColor controlTextColor]]; } } // setEnabled
This actually kind of a gross workaround for a Cocoa bug - the disabled color is getting made darker rather than lighter. The secondarySelectedControlColor ends up looking disabled by a happy coincidence that it starts out lighter before being darkened. (or something like this. UberDude Dave MacLachlan has done the legwork to figure out the underlying problem.)
[[textfield cell] setLineBreakMode: NSLineBreakByTruncatingMiddle](Thanks to Daniel Jalkut for this one)
NSRange zeroRange = { 0, 0 };
[textView scrollRangeToVisible: zeroRange];
NSString *commitMessage; commitMessage = [[commitTextView textStorage] string];