如何复制self.title?

时间:2016-01-19 05:14:23

标签: ios objective-c

我将页面标题设置为:

df1 <- data.frame(year=2015, month=1:12)

现在我想启用此标题,因为该用户可以复制此标题。

那我该怎么做?

修改

self是我的self.title = @"My Title"; 课程,我的观点位于UIViewController

当用户持有标题时,它可以显示工具提示,例如“复制”和复制的标题文本。

5 个答案:

答案 0 :(得分:1)

这是方法:

创建CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.fillRule = kCAFillRuleEvenOdd; maskLayer.frame = img_profilepic.bounds; CGFloat width = img_profilepic.frame.size.width; CGFloat height = img_profilepic.frame.size.height; CGFloat hPadding = width * 1 / 8 / 2; UIGraphicsBeginImageContext(img_profilepic.frame.size); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(width/2, 0)]; [path addLineToPoint:CGPointMake(width - hPadding, height / 4)]; [path addLineToPoint:CGPointMake(width - hPadding, height * 3 / 4)]; [path addLineToPoint:CGPointMake(width / 2, height)]; [path addLineToPoint:CGPointMake(hPadding, height * 3 / 4)]; [path addLineToPoint:CGPointMake(hPadding, height / 4)]; [path closePath]; [path closePath]; [path fill]; [path stroke]; maskLayer.path = path.CGPath; UIGraphicsEndImageContext(); img_profilepic.layer.mask = maskLayer; img_profilepic.image=[UIImage imageNamed:@"profile_picture"]; 子类,并将此方法添加到类中。

TitleLabel.h文件:

UILable

和TitleLabel.m文件:

#import <UIKit/UIKit.h>

@interface TitleLabel : UILabel

@end

现在将此标注添加到#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 navigation,例如:

titleView

这里你可以长篇大论地复制TitleLabel *title = [[TitleLabel alloc] initWithFrame:CGRectMake(0, 0, 150, 50)]; title.textAlignment = NSTextAlignmentCenter; title.text = @"My title"; title.userInteractionEnabled = YES; self.navigationItem.titleView = title;

答案 1 :(得分:0)

您可以将名为'YourTitleLabel'的UILabel子类化,并将其指定为UIViewController的navigationItem的titleView。

self.navigationItem.titleView = YourTitleLabel;

你应该覆盖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

希望对你有所帮助!

答案 2 :(得分:0)

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.title;

请参阅此链接:https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/UsingCopy,Cut,andPasteOperations/UsingCopy,Cut,andPasteOperations.html

答案 3 :(得分:0)

您可以在self.navigationbar.titleview中设置自定义标签,并在自定义标签中设置标题。如果那样也行不通。那么你可以尝试使用uitextfield(实际上在titleview中设置textfield很奇怪),不可编辑。

答案 4 :(得分:0)

你需要创建一个sampleLabel类,它是UILabel的子类,如下所示,你需要复制文本选项使用UILabel的这个SampleLabel intead

@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