[Quickies]Getting UITextView to dismiss keyboard when the user hits the return key

No Comments

It might seem something simple, but I still remember when I was beginning my life as an iOS developer and certain things were not exactly out of the box. UITextView’s support for multiple lines puts it into a complicate situation when it comes to handle the return key. Still, that’s something you can easily implement using a delegate method.

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if([text isEqualToString:@”\n”]){

[textView resignFirstResponder];

return NO;
}
return YES;

}

[Quickies]Putting a NSXMLDocument into a NSTextView

Comments Off on [Quickies]Putting a NSXMLDocument into a NSTextView

Say you want to display a XML to the user on a NSTextView. You have a NSXMLDocument called “xmldocument” and you need to put into a NSTextView(you created an IBOutlet called “xmlTextView” in order to make your code interact with the NSTextView). You’d just use this code:

//Convert the NSXMLDocument to NSData
NSData *data = [xmldocument XMLDataWithOptions:NSXMLNodePrettyPrint];
//Create a string from the NSData object you have
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//Create a NSAttributedString from it
NSAttributedString *attributedString = [[[NSAttributedString alloc] initWithString:string] autorelease];
//Now set the attributed string for the NSTextView
[[xmlTextView textStorage] setAttributedString:attributedString];