Localizing attributed UITextView from storyboard - ios

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 extract strings from the storyboard for localization and afterwards use ibtool -write on another storyboard file to apply the translated strings.
When I use ibtool any UITextViews that have attributed text is ignored by the ibtool --generate-strings-file command and omitted from the resulting strings file.
Is it possible to extract attributed text from a storyboard for localization?

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!!
}

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.

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.

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!

Related

How can I add a custom view to an attributed string or text view?

I'm trying to get a custom view in an attributed string to be displayed on a textView. I am able to add an image with an NSTextAttachment, but it isn't what I want. I have a custom view that supports Gif's and Animated PNGs that I'd like to display between text.
Example:
text text text [customView] text [customView] text. <- In text view, preferably in attributed string
I would love some guidance as to where I should search specifically. So far I've seen related issues...
Subclass NSTextAttachment: How to subclass NSTextAttachment?
Use NSTextAttachmentContainer..?
NSTextAttachmentCell - Only OSX
Do manipulation in the text view
First, use NSAttributedString or NSMutableAttributedString to show your RichText in subviews (such as UITextView/UILabel)
Then, use NSTextAttachment to replace your image-script in text.
NSString *egText = #"hello [this_is_an_img_script]";
NSMutableAttributedString * destStr = [[NSMutableAttributedString alloc] initWithString:egText];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:[this_is_an_img_script]];
NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; //make your image to an attributedString
[destStr replaceCharactersInRange:range withAttributedString:textAttachmentString];
at last: [YourLabel(or YourTextView) setAttributedString:destStr];
BTW: if you use the YYkit for the RichText, you cannot use the YYAttachMentString to replace NSAttachMentString, these are different things, the UITextView(UILabel) cannot load the YYAttachMentString.
I'm looking for some way to show my gifs with the UITextView (because YYKit cannot load and preview netimage with a url, YYKit always show empty which should be a netImage, cripes!)

When tapping a custom URL link in a UITextView the delegate receives nil URL

I have a tableView with tableViewCells cell that show a textView.
textView uses an attributedString with custom URL link information, set up in tableView:cellForRowAtIndexPath: as shown in this tutorial:
NSMutableAttributedString *attributedDisplayString = [[NSMutableAttributedString alloc] initWithString:displayString];
[attributedDisplayString addAttribute:NSLinkAttributeName
value:[NSString stringWithFormat:#"username://%#", userName]
range:NSMakeRange(0, userName.length)];
cell.textView.attributedText = attributedDisplayString;
When I tap the link, textView:shouldInteractWithURL:inRange: is called in the delegate, thus the custom URL link has been detected and responds.
However, the supplied URL is nil.
What am I missing?
Sorry for asking too fast. I found the problem, but maybe this helps others:
My variable userName simply contained a space, and could thus not be converted to a URL.
After removing the space, it works.
To make a string that can be used for a URL, one can use in iOS8 and earlier
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
and in iOS9
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]

Attributes not showing up in UITextView (iOS 8/Xcode 6.4)

I'm trying to load an RTF file and display it's contents inside a UITextView using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.allowsEditingTextAttributes = YES;
NSURL *rtfpath = [[NSBundle mainBundle] URLForResource:#"rtfFile" withExtension:#"rtf"];
NSAttributedString *rtfText = [[NSAttributedString alloc] initWithFileURL:rtfpath options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
self.textView.attributedText = rtfText;
}
The text loads fine, but none of the attributes (size, color, bold, font) show up and it just displays the text using a basic system font. This is a relatively large file that will be edited frequently so is there any I way I can display attributes (for example I want the section headings to be bold) without manually hard coding every line of text?
Also I've tried manually pasting the contents of my file into the attributed text field of the UITextView in the storyboard. It actually displays everything with attributes in the storyboard, but when I run the code all my attributes disappear again.

Share image in iMessage using Custom Keyboard in iOS 8

I am working on iOS 8 custom keyboard, where i have designed keyboard using some images like smiley. i want this keyboard to be work with iMessage. when i am trying to send text its working properly but can't able to share image there. I have tried following code :
To share text : (its working properly)
-(void)shouldAddCharector:(NSString*)Charector{
if ([Charector isEqualToString:#"Clear"]) {
[self.textDocumentProxy deleteBackward];
} else if([Charector isEqualToString:#"Dismiss"]){
[self dismissKeyboard];
} else {
[self.textDocumentProxy insertText:Charector];
}
}
To add image : ( Not working)
-(void)shouldAddImage:(UIImage*)oneImage
{
UIImage* onions = [UIImage imageNamed:#"0.png"];
NSMutableAttributedString *mas;
NSTextAttachment* onionatt = [NSTextAttachment new];
onionatt.image = onions;
onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height);
NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];
NSRange r = [[mas string] rangeOfString:#"Onions"];
[mas insertAttributedString:onionattchar atIndex:(r.location + r.length)];
NSString *string =[NSString stringWithFormat:#"%#",mas];
[self.textDocumentProxy insertText:string];
}
Is there any possibility to pass image to [self.textDocumentProxy insertText:string];
following attached image shows how exactly i want to use this image keyboard. i am surprised how emoji keyboard will work?
As far as I know, the behavior you are looking for is not possible as of iOS 8 beta 4.
Currently, the only way for iOS custom keyboards to interact with text is through <UITextDocumentProxy> and the only way to insert anything is via the insertText: method.
Here is the header for the insertText: method in <UITextDocumentProxy>:
- (void)insertText:(NSString *)text;
As you can see, it takes a plain NSString... not an NSAttributedString. This is why your attempt to insert an image doesn't work.
However, despite the fact that you can't add pictures, it is still very possible to insert emojis, since an emoticon is really just a Unicode character.
To add an emoji, just insert the proper Unicode character:
[self.textDocumentProxy insertText:#"\U0001F603"];
Useful links:
List of Unicode Emoji: http://apps.timwhitlock.info/emoji/tables/unicode
Unicode Characters as NSStrings: Writing a unicode character with NSString

UISegmentedControl and localization

I have a .xib file, with accompanying .strings files for different languages. The .xib file contains a label, and a UISegmentedControl.
When asking IB to localize the .xib file, I get the following .strings file:
"6.segmentTitles[0]" = "title1";
// ...More strings related to the segmented control...
"11.text" = "bla";
The 'bla' string belongs to the label.
Changing the 'bla` string is reflected in runtime, while changing the 'title1' string does not. Anyone knows why?
This question is not new, but as it is still without any answers I will add my solution as it may help others.
Generally, as it's mentioned above, it is an open bug that UISegmentedControl segment titles do not pick up localization strings.
- (void)viewDidLoad
{
...
// Locale will be picked automatically by NSBundle.
NSString *resourcePath =[[NSBundle mainBundle] pathForResource:#"MainStoryboard" ofType:#"strings"];
NSDictionary *resourceDict = [NSDictionary dictionaryWithContentsOfFile:resourcePath];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[0]"] forSegmentAtIndex:0];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[1]"] forSegmentAtIndex:1];
}
Where COo-BO-Ryl is the Object ID of segmentedControl.
Not very pretty, but does the job.

Resources