[英]Writing NSAttributedString to Dropbox file iOS
如何將屬性字符串寫入Dropbox文件? 我正在嘗試為我的文件創建標題,但我不知道如何更改Dropbox文件中的文本格式。
就像是:
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沒有直接讀取/寫入文件的方法。 但是它確實符合NSCoding協議,因此可以使用以下代碼將其序列化為文件:
[NSkeyedArchiver archiveRootObject: string toFile: myFilePath];
然后再讀一遍:
NSAttributedString *string =
[NSKeyedUnarchiver unarchiveObjectWithFile: myFilePath];
請注意,文件格式不適合與其他平台/應用程序交換樣式化文本。 如果使用NSkeyedArchiver歸檔屬性字符串,則對結果文件只能執行的操作是使用NSKeyedUnarchiver取消歸檔該字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.