Writing NSAttributedString to Dropbox file iOS - ios

How can I write an attributed string to a Dropbox file? I'm trying to create a header for my file, but I don't know how to change the text format within the Dropbox file.
Something like:
DBPath *newPath = [[DBPath root] childPath:#"hello.txt"];
DBFile *file = [[DBFilesystem sharedFilesystem] createFile:newPath error:nil];
UIFont *font = [UIFont fontWithName:#"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:#"Title" attributes:attrsDictionary];
if (file)
[file writeString:string error:nil];

NSAttributedString does not have methods to read/write to a file directly. However it does conform to the NSCoding protocol so it's possible to serialize it to a file, with code like this:
[NSkeyedArchiver archiveRootObject: string toFile: myFilePath];
And then to read it back:
NSAttributedString *string =
[NSKeyedUnarchiver unarchiveObjectWithFile: myFilePath];
Note that the file format is not suitable for exchanging styled text with other platforms/applications. If you archive an attributed string using NSkeyedArchiver, the only thing you can do with the resulting file is to unarchive it with NSKeyedUnarchiver;

Related

Saving NSAttributedString to Parse.com

Parse doesn't support direct saving of NSAttributedStrings. Converting to HTML isn't the most straightforward. Anyone have a friendly method for storing NSAttributedStrings (font & superscript) to Parse.com?
Thanks guys, decided to go with saving an rtf string onto Parse, based off #Wain's comment.
// convert NSAttributedString to RTFString and save to Parse
PFObject *note = [PFObject objectWithClassName:#"Note"];
NSAttributedString *noteAttributedText = self.noteTextView.attributedText;
NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSRTFTextDocumentType,NSDocumentTypeDocumentAttribute, nil];
NSData *rtfData = [noteAttributedText dataFromRange:NSMakeRange(0, noteAttributedText.length) documentAttributes:documentAttributes error:NULL];
NSString *rtfString = [[NSString alloc] initWithData:rtfData encoding:NSUTF8StringEncoding];
note[#"noteTextAsRTFString"] = rtfString;
// convert RTFString to NSAttributedString after pulling from Parse
NSString *rtfString = [pfObject objectForKey:#"noteTextAsRTFString"];
NSData *data = [rtfString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *noteAttributedText = [[NSAttributedString alloc] initWithData:data options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];

NSAttributedString to NSData No visible #interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:

I am trying to convert NSAttributedString to NSData using RTFDFromRange method. Getting this:
No visible #interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:
What is wrong with my code?
NSAttributedString *val=self.textview.attributedText;
NSData *data = [val RTFDFromRange:NSMakeRange(0, self.textview.text.length) documentAttributes:nil];
NSAttributedString does not have a method called RTFDFromRange for iOS, but only for Mac OS X.
To convert NSAttributedString to NSData in iOS, you can try these two approaches:
1. Using initWithData:
NSMutableAttributedString *val = [[NSMutableAttributedString alloc] initWithData:data options:nil documentAttributes:nil error:nil];
2. Using NSKeyedArchiver:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: val];
And to convert the NSData back to a string:
NSAttributedString *val = [NSKeyedUnarchiver unarchiveObjectWithData: data];
This code works on both Mac and iOS.
See the Apple docs here.
That method is only available under Cocoa (OSX) as it's part of the AppKit Additions to NSAttributedString.
Here's an open source category that might do what you want under iOS (not personally tested, however).

Objective C - How to create rtf from NSAttributedString

I can convert from rtf string to attributed string using following:
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
Now how can i convert back from attributedString to rtf string?
You want to use -dataFromRange:documentAttributes:error:
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"YOLO" attributes:nil];
NSData *data = [str dataFromRange:(NSRange){0, [str length]} documentAttributes:#{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:NULL];
[data writeToFile:#"/me.rtf" atomically:YES];
Of course you'd want to have some attributes instead of "YOLO", but you get the idea.
Also, if you're looking to simply write this to disk, then fileWrapperFromRange:documentAttributes:error:
might even be a better option. You can find more about reading and writing from the Attributed String Programming Guide

How to load only Custom font names in Array?

I am using Multiple custom fonts in my app.i know how to Add custom font but the Terrible parts of all this process is when we are going to use the custom font because mostly the custom font name is different from the file that we are using in app.here i would like to explain the recent example that i did,First of all i have added two custom font file in my app
For getting the above custom font names i tried this way
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
FontnameArray=[[NSMutableArray alloc] init];
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSString *fullName = [NSString stringWithFormat:#"%#",[fontNames objectAtIndex:indFont]];
[FontnameArray addObject:fullName];
}
}
[FontnameArray sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#" Font name: %#", FontnameArray);
The above Code display 199 font names for me ,so it was to difficult for me to pick the custom font name among 199 fonts, To make my doubt clear then i have simply drag the font file to font book Then i got the orignal names of custom Font.
[UIFont fontWithName:#"Ethiopia Primary" size:20]];
[UIFont fontWithName:#"Ethiopia jiret" size:20]];
So the Question is how can i get all Custom font names in Array like Above instead of draging the each font file to Fontbook for getting orignal names.
Just compare with all fonts exist in iOS
*Create new project like this code and write array of all font names to a plist file
- (void)writeAlliOSFontsToPlist{
NSArray *fontFamilies = [UIFont familyNames];
[self savePlist:#"AllFont" fromArray:fontFamilies];
}
- (NSString *) getFilePathForPlist:(NSString *) plistName {
// placeholder
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",plistName]];
}
-(void) savePlist:(NSString *)plistName fromArray:(NSArray *)array {
[array writeToFile:[self getFilePathForPlist:plistName] atomically:YES];
}
Open your simulator dir and copy this plist to your App bundle.
Then read this plist to an array and compare with all of your fonts by this code
-(void)dumpCustomFonts{
NSArray *iOSFontsArray = [NSArray arrayWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"AllFont.plist"]];
NSMutableArray *fontFamilies = (NSMutableArray *)[UIFont familyNames];
[fontFamilies removeObjectsInArray:iOSFontsArray];
NSLog(#"%#",fontFamilies);
}
I've done all in my test project, you just copy and paste it.
You should add your custom font in .plist by
1) Add a new entry with the key "Fonts provided by application".
2) Now add each custom font file name with extension to this array like :
Now you can retrive all your custom fonts by :
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
fontFamilyNames = [plistData valueForKey:#"UIAppFonts"];
NSLog(#"%#",fontFamilyNames);
NSString *fontFamilyName = [[fontFamilyNames objectAtIndex:index] stringByDeletingPathExtension];
UILabel *fontNameLabel = [[[UILabel alloc] init] autorelease];
fontNameLabel.text = #"Preview Text";
fontNameLabel.font = [UIFont fontWithName:fontFamilyName size:20.0];

How to convert formatted content of NSTextView to string

I need transfer content of NSTextView from Mac app to iOS app. I'm using XML as transfered file format.
So I need to save content of NSTextView (text, fonts, colors atd.) as a string. Is there any way how to do that?
One way to do this is to archive the NSAttributedString value. Outline sample code typed directly into answer:
NSTextView *myTextView;
NSString *myFilename;
...
[NSKeyedarchiver archiveRootObject:myTextStorage.textStorage
toFile:myFilename];
To read it back:
myTextView.textStorage.attributedString = [NSKeyedUnarchiver unarchiveObjectWithFile:myFilename];
That's all that is needed to create and read back a file. There are matching methods which create an NSData rather than a file, and you can convert an NSData into an NSString or just insert one into an NSDictionary and serialise that as a plist (XML), etc.
Your best bet is probably to store the text as RFTD and load it as such in the other text view via an NSAttributedString.
// Load
NSFileWrapper* filewrapper = [[NSFileWrapper alloc] initWithPath: path];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper: filewrapper];
NSAttributedString* origFile = [NSAttributedString attributedStringWithAttachment: attachment];
// Save
NSData *data = [origFile RTFDFromRange: NSMakeRange(0, [origFile length]) documentAttributes: nil];
[[NSFileManager defaultManager] createFileAtPath: path contents: data attributes:nil];

Resources