Created
April 29, 2016 08:35
-
-
Save jsbain/b2436247405bb2ee81c1a52f5f2f3e83 to your computer and use it in GitHub Desktop.
rtf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from objc_util import * | |
| import ui | |
| def save_text(tv,filename): | |
| '''save the attributed text from tv to an rtf file (provide full path)''' | |
| attribtxt=ObjCInstance(tv).attributedText() | |
| data=attribtxt.RTFFromRange_documentAttributes_(NSRange(0,attribtxt.length()), None) | |
| data.writeToFile_atomically_(filename,True) | |
| @on_main_thread | |
| def load_text(tv,filename): | |
| '''load an rtf file, and load into the attributed text f the textview. (provide full path) | |
| ''' | |
| loaded_data=NSData.dataWithContentsOfFile_(filename) | |
| nas=ObjCClass('NSAttributedString') | |
| newtext=nas.alloc().initWithRTF_documentAttributes_(loaded_data,None) | |
| ObjCInstance(tv).attributedText=newtext | |
| if __name__=='__main__': | |
| tv=ui.TextView() | |
| tv.text='hello' | |
| tv.font=('Snell Roundhand',46) | |
| filename=os.path.abspath('myfile.rtf') | |
| save_text(tv,filename) | |
| tv2=ui.TextView() | |
| load_text(tv2,filename) | |
| tv2.present('sheet') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment