NSTextView extract raw text content - objective-c

I want to find something that is the equivalent of [fieldName stringValue] for NSTextField, but for NSTextView. My understanding is that the stringValue property doesn't exist for the NSTextField because this field supports richer text.
Despite much googling I can't figure out how to just get the raw text content of the NSTextView. The only reason I'm using the NSTextView and not NSTextField is so I can have scroll bars...
Definitely a newb question!
Thanks in advance.

NSTextField is a subclass of NSText, which has a "string" method. Try this:
[myTextField string];
It should work as needed.

Related

Making invisible characters visible in NSTextField

I have a NSTextField and I want to give the user the opportunity to make invisible characters like blanks, carriage returns and tabs visible. Unfortunately I didn't find a word in Apple's documentaion about this. I assume I'm not using the right term when looking for it.
Any clues how to do this?
First, I'd go for a NStextView instead, where the associated NSLayoutManager and NSTextStorage components are already set for you. Then, you can achieve what you are trying to do by doing the following steps:
Subclass NSATSTypesetter to draw custom glyphs for any characters you
want by overriding :
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin
- (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex
Subclass NSLayoutManager and set its type setter with the above one.
Then override:
-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin
{
[self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
}
Replace the layout manager of the NSTextView with the above one:
[[textView textContainer] replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];
Basically, you have to check NSLayoutManager and NSATSTypesetter classes for anything related to text custom drawing. Also there is a detailed guide about all this here.

How to get highlighted text from my NSTextField?

It must be certainly easy but I haven't found anything in the doc.
The behavior I need is exactly as when you right-click any portion of text and then you can do some action with it.
For the moment I have a my own custom NSTextField class which re-implment 'mouseDown' action. This part works ! I thought I could get the selected portion of text of my nstextfield thanks to 'theEvent' but apparently it is not possible.
I don't actually think you can do this with NSTextField. I think you have to use an NSTextView instead and then make use of the -selectedRanges method.
EDIT: I should have said, you can't do this directly (i.e., there's no NSTextField method for doing this). I think rather, you have to use the field editor (which is itself an NSTextView) associated with the window in order to do this. Here's the apple guide for using the field editor.
NSString *selectedText = ((NSTextFieldCell*)textField.selectedCell).stringValue;

How to configure a NSTextView with a NSTextStorage?

How do you store the NSTextView's information with an NSTextStorage?
See Assembling the Text System by Hand in the Cocoa Text Architecture Guide.
Apparently each NSTextView comes with it's own NSTextStorage already. Just use the textStorage method to get the textStorage of the NSTextView

Problems with NSTextView where NSTextField worked

I'm just starting out with obj-c, I'm trying to build an importer to fetch a couple numbers from a piece of formatted text. I started with a wrapping TextField and was able to both get the text into a string and search it as I wanted with
NSString *varImport = [NSString stringWithString:[importTextView stringValue]];
When I switch over to the TextView in the Interface Builder I get there error
-[NSScrollView string]: unrecognized selector sent to instance 0x100429160
I think this may be the root of my problem, although I am dragging over a TextView when I look in the inspector panel its labeled as a ScrollView which I'm not familure with yet.
Through my research I came across two different sites saying that TextView wont directly feed into a string but it was for odd reasons, IE TextView stored the data as a MutableString that was constantly updating and to access it you had to copy the original (Second example) Anyways I'm turning to the experts because I'm clearly doing something wrong and I can't make sense of the answers on the web.
NSString *varImport = [NSString stringWithString:[[import textStorage] string]];
NSString *varImport = [[import string] copy];
Don't worry so much about the other stuff if you don't have the time to explain what's going in the web examples, I mainly want to know about the ScrollView stuff and how to get a string out of it to be able to search it.
Thanks in advance!
Graham
Simply because you are using the wrong method.
NSString *varImport = [NSString stringWithFormat:[importTextView string];
Also, you might have connected the importTextView instance variable to the wrong connection. A NSTextView is always in a NSScrollView. Simply just right-click your object from interface builder and drag the instances to the top of the textview (Where it should show "NSTextView").
Save the nib and try run it again. It should work.
To get text value from a text field or text view you should use - (NSString *)string method.

Objective-C, Set UITextField Value from this:

I have been told that to access a UITextField in the setup i have with my app, i have to use a subView whatever this is (in a noob at objectivec) and i was given this
[subview isKindOfClass:[UITextField class]]
Where do i put this? And how can i use it to set the value of my UITextField? :D
Thanks!
Update: I want to set the value of my textfield, in the function that is called after my URLScheme - the scheme bit works as i have alerted out the url.. this is cool, now i need to set a textfield with a string?
Iterating through subviews and choosing based on type is not generally considered good design practice. You should reconsider how this is structured to have explicit access to the UITextField you want to access. Doing this with Apple's private classes in particular will deny you access to the App Store. At any rate, here's how you'd do what you want:
for(UIView *subview in myParentView){
if([subview isKindOfClass:[UITextField class]])
[(UITextField *)subview setText:#"MyString"];
}
This is still very little useful context, but you most likely want to create an outlet to your text field and just set the value using that reference (with the text property).