app-specific UIPasteboard - ios

I'm trying to create app-specific UIPasteboard, but have some problems figuring out how to register it:
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:#"myPasteboard" create:YES];
If I just create it, app continues to use generalPasteboard. I really hope that I don't have to catch cut/copy/paste operations and set items manually.

Please read this article
http://nshipster.com/uimenucontroller/ (It's about UILabel, not UITextField but is useful just for example and information about how copy/paste/edit actions work and how you can customize its' behavior)
I think you need to subclass UITextField to MyTextField for example, and override -(void)copy: and -(void)paste: methods from UIResponderStandardEditActions.
In your implementation of these methods you should use your custom UIPasteboard to copy text into and paste from it, like mentioned in this answer,
How to do copy/paste function programmatically in iphone?
but instead of using [UIPasteboard generalPasteboard] use [UIPasteboard pasteboardWithName:#"myPasteboard" create:YES]

//Copy your text field text and Just App in Pasteboard
NSString * text=textfield.text;
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
[pasteboard setString:text];
Below is a Tutorial about Pasteboard.
http://hayageek.com/uipasteboard-example-read-write-share/

//pasteboard initialization
let pasteBoard = UIPasteboard.general
// copying the content of textview to pasteboard
if(pasteBoard.hasStrings){
pasteBoard.string = textViewPB.text
}
//pasting the content of pasteboard to textview
if(pasteBoard.hasStrings){
textViewPB.text = pasteBoard.string
}

Related

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]

Custom GIF Keyboard in iOS8

I am creating Custom GIF Keyboard in iOS8 using app extension. I was created layout of my custom keyboard. I was implement LongPress Gesture for selecting GIF Image but its not work. so what can i do for this or any suggestion regarding to this?
- (void)textWillChange:(id<UITextInput>)textInput {
[self.textDocumentProxy insertText:#"Hi"];
}
I also tried with UITextInputDelegate mentioned above.
This method is also deals with only text append. But My concern is to load gif or png image in input area.
I have done with long press gesture and make a method for touch handler.
-(void)tapped:(id)sender
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *image = [UIImage imageNamed:#"img_temp.gif"];
NSData *imgData = UIImagePNGRepresentation(image);
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
}
I also given become first responder aswell.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
I can copy text in pasteboard, but I can't copy .gif or .png image.
What Popkey, for example, are doing is copy the clicked GIF to the pasteboard.
The user then needs to long click their rich text field and paste the content into their iMessage for example.
You couldn't load a gif directly in the input area. To do that you have to copy the gif to the pasteboard and then paste the gif in the input area.
To copy the gif into the pasteboard you need to convert it into the NSData.
Here is the demo code in Swift 3.0.
let pb = UIPasteboard.general
let data = NSData(contentsOfURL: url)
if data != nil
{
self.pb.setData(data!, forPasteboardType: kUTTypeGIF as String)
}

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

Localizing attributed UITextView from storyboard

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!

How to paste text into UITextView in iOS?

I have a UITextView and i want to paste text into that UITextView with button's click event.
You know , when i click a button , the copied text from anywhere paste into that UITextView.
How can i do that?
Thanks in advance.
-(IBAction)buttonPressed:(id)sender
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
MsgtextView.text = [NSString stringWithFormat:#"%# %#",MsgtextView.text,pasteboard.string;
}
Check this
You can use the UIPasteboard class.
Try this code
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
textView.text = pasteboard.string;

Resources