Localizing attributed UITextView from storyboard

后端 未结 4 1963
梦谈多话
梦谈多话 2021-01-01 16:00

I am using the storyboard and have a view where I have subclassed some UITextViews.

My problem is that I am using ibtool --generate-strings-file to extr

相关标签:
4条回答
  • 2021-01-01 16:24

    I ended up concluding that it couldn't be done using ibtool in the present version.

    Instead I let the Textview be a plain text view and, using my subclass, parsed its text property so I could make a NSMutableAttributedString and set the needed properties.

    That enabled me to use ibtool to extract the strings and still have an attributed textview.

    0 讨论(0)
  • 2021-01-01 16:30

    It wouldn't make sense to localize attributed text from the storyboard as you would need to know where to apply the attributes after the translation. The word order might have changed and where you would have specified some blue bold text for instance might no longer make sense.

    You can of course still do it via code where you can split up your string in multiple ones and localize them individually. Then you can specify setAttributes:range: so that you know that the right attributes will always be applied on the right range of the string.

    0 讨论(0)
  • 2021-01-01 16:31

    Here's a workaround which I find quite useful: just create a ViewController with the translated TextView. In details (here I just translate the attributed text into english):

    1) Crete a new Controller with "New File" -> "UIViewController with Xib". Name it "AttributedTranslated" and fill it with just a TextView with the attributed text being translated, using Interface Builder.

    2) In your main controller .m file, write down the following method:

    - (BOOL)isEng {
        return [[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"];
    }
    

    3) Define an "AttributedTranslated" object and a View in your .h file

    IBOutlet UIView            *attrView;    
    AttributedTranslated       *attr;
    

    4) On the xib file (or storyboard) of your main controller, create a View containing just the attributed textView (in the original language) and link it to "attrView".

    5) On you viewDidLoad do something like the following:

    if ([self isEng]) {
    
        desc = [[Description alloc] init];
        [attrView addSubview:attr.view];
    }
    

    Maybe it's not the best way to do it, but it does allow one to translate the attributed text in Interface Builder, easily!

    0 讨论(0)
  • 2021-01-01 16:38

    on Xcode 6.1, the best way is to copy the attributed text of a text view into a “BASE” RTF text ( using TextEdit for example or directly from XCode > New File > ressources > RTF ).

    Going through the TextEdit way, you need to import your text into your project. Obviously, if you have done it through Xcode, nothing to import.

    then just you use the utilies panel to find the “localize..." button which will do it's deed for you.

    to import the correct version just do ( in viewWillAppear for ex. ),

     NSURL *url = [[NSBundle mainBundle] URLForResource:[fileName stringByDeletingPathExtension] withExtension:[fileName pathExtension]];
    NSError *error;
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithFileURL:url
                                                                               options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
                                                                    documentAttributes:nil
                                                                                 error:&error];
    
    [_originalMessage setAttributedText:attributedString];
    

    Update for Swift 4:

    var attrString: NSAttributedString?
    let fileUrl: URL = Bundle.main.url(forResource: "mytextfile", withExtension: ".rtf")!
    
    do {
       attrString = try NSAttributedString(url: fileUrl, options: [.documentType:NSAttributedString.DocumentType.rtf], documentAttributes: nil)
    } catch {
        // Somebody do something!!        
    }
    
    0 讨论(0)
提交回复
热议问题