0

I'm setting my page title as:

self.title = @"My Title";

Now I want to enable this title as that user can copy this title.

So How do I do that ?

EDIT:

self is my UIViewController class and my view is in navigation controller.

When user hold the title it can show tooltip like "Copy" and title text copied.

Dx Android
  • 61
  • 2
  • 13
  • check this . https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/UsingCopy,Cut,andPasteOperations/UsingCopy,Cut,andPasteOperations.html – Badal Shah Jan 19 '16 at 05:18
  • Please clarify what you mean. What is `self` in your question? How do you want the user to copy the title? – rmaddy Jan 19 '16 at 05:20
  • its my UIViewController. – Dx Android Jan 19 '16 at 05:21
  • https://zearfoss.wordpress.com/2013/06/02/mastering-copy-and-paste-in-ios-part-2/ – Rohit Khandelwal Jan 19 '16 at 05:25
  • You still haven't explained what you want the user to be able to do. Don't just say "copy the title". How do you want then to be able to copy the title? – rmaddy Jan 19 '16 at 05:42
  • Possible duplicate of [How do you copy a string onto the clipboard (Pasteboard) in iOS?](http://stackoverflow.com/questions/13054014/how-do-you-copy-a-string-onto-the-clipboard-pasteboard-in-ios) – Cristik Jan 19 '16 at 07:15

5 Answers5

0

You can subclass UILabel named 'YourTitleLabel' and assign it as titleView of UIViewController's navigationItem.

self.navigationItem.titleView = YourTitleLabel;

And you should override several methods of UIResponder.

@interface YourTitleLabel : UILabel

@end

@implementation YourTitleLabel

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:)) {
        return YES;
    }
    return NO;
}

- (void)copy:(id)sender
{
    [[UIPasteboard generalPasteboard] setString:self.text];
    [self resignFirstResponder];
}

@end

Hope helpful for you!

SeraZheng
  • 187
  • 8
0
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.title;

Refer to this link: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/UsingCopy,Cut,andPasteOperations/UsingCopy,Cut,andPasteOperations.html

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Satyanarayana
  • 1,059
  • 6
  • 16
0

Here is the way :

Make UILable subclass, and add this methods to the class.

TitleLabel.h file:

#import <UIKit/UIKit.h>

@interface TitleLabel : UILabel

@end

and TitleLabel.m file:

#import "TitleLabel.h"

@implementation TitleLabel

- (void)initCommon
{
    UILongPressGestureRecognizer *lpRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [self addGestureRecognizer:lpRecognizer];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initCommon];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initCommon];
    }
    return self;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)copy:(id)sender
{
    [[UIPasteboard generalPasteboard] setString:self.text];
    [self resignFirstResponder];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
    UIMenuController *menu = [UIMenuController sharedMenuController];
    if (![menu isMenuVisible])
    {
        [self becomeFirstResponder];
        [menu setTargetRect:self.frame inView:self.superview];
        [menu setMenuVisible:YES animated:YES];
    }
}

@end

Now add this lable to your navigation titleView like :

TitleLabel *title = [[TitleLabel alloc] initWithFrame:CGRectMake(0, 0, 150, 50)];
title.textAlignment = NSTextAlignmentCenter;
title.text = @"My title";
title.userInteractionEnabled = YES;
self.navigationItem.titleView = title;

and here is you can copy self.title on long press on lable.

iBhavin
  • 1,261
  • 15
  • 30
0

you can set your custom label in self.navigationbar.titleview and set title in your custom label. If that will also not work. then you can try with uitextfield (actually it is weird to set textfield in titleview) with non-editable.

Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
0

you need to create one sampleLabel class, which is the sub class of UILabel as follows , where you required copy text option use this SampleLabel intead of UILabel

@implementation SampleLabel UILabel
- (void) awakeFromNib
{
   [super awakeFromNib];
   [self attachTapHandler];
}


- (void) attachTapHandler
{
if (self.tag != 100 ) {
    [self setUserInteractionEnabled:YES];
    UITapGestureRecognizer *touchy = [[UITapGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(handleTap:)];
    touchy.numberOfTapsRequired =  2;
    [self addGestureRecognizer:touchy];
}

}
- (void) copy: (id) sender
{
  [[UIPasteboard generalPasteboard] setString:self.text];
}

- (BOOL) canPerformAction: (SEL) action withSender: (id) sender
{
  return (action == @selector(copy:));
}

- (void) handleTap: (UIGestureRecognizer*) recognizer
{
 [self becomeFirstResponder];
 UIMenuController *menu = [UIMenuController sharedMenuController];
 [menu setTargetRect:self.frame inView:self.superview];
 [menu setMenuVisible:YES animated:YES];
}

- (BOOL) canBecomeFirstResponder
{
 return YES;
}


  @end
Satyanarayana
  • 1,059
  • 6
  • 16