iOS開發之復制字符串到剪貼板


概述

一般有邀請復制鏈接需求功能,把字符串復制到系統剪貼板,供用戶粘貼使用鏈接.

詳細

截圖.jpeg

一、主要思路

 

  • 1.在 View 里貼上scrollView

  • 2.在scrollView里貼上 UITextView,用於上下滑動展示完整數據

二、程序實現

1、在 View 里貼上scrollView

- (void)setupUI {
    
    CGFloat marginX = 15;
    CGFloat cellH = 50;
    CGFloat btnW = 70;
    CGFloat btnH = 30;
    
    // 邀請鏈接
    UIView *linkView2 = [[UIView alloc] init];
    linkView2.backgroundColor = [UIColor whiteColor];
    linkView2.frame = CGRectMake(0, 100, UI_View_Width, cellH);
    [self.view addSubview:linkView2];
    // 邀請鏈接label
    UILabel *linkLabel2 = [[UILabel alloc] init];
    linkLabel2.backgroundColor = [UIColor clearColor];
    linkLabel2.frame = CGRectMake(marginX, -1, 60, cellH);
    linkLabel2.text = @"邀請鏈接";
    linkLabel2.font = [UIFont systemFontOfSize:14];
    linkLabel2.textColor = ZLColor(102, 102, 102);
    [linkView2 addSubview:linkLabel2];
    // 復制按鈕
    UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [copyBtn setTitle:@"復制" forState:UIControlStateNormal];
    copyBtn.frame = CGRectMake(UI_View_Width - marginX - btnW, (cellH - btnH) * 0.5, btnW, btnH);
    copyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [copyBtn addTarget:self action :@selector(copylinkBtnClick) forControlEvents:UIControlEventTouchUpInside];
    copyBtn.backgroundColor = [UIColor whiteColor];
    [copyBtn setTitleColor:ZLColor(108, 187, 82) forState:UIControlStateNormal];
    copyBtn.layer.borderColor = ZLColor(108, 187, 82).CGColor;
    copyBtn.layer.cornerRadius = 3.0;
    copyBtn.layer.borderWidth = 1.0f;
    [linkView2 addSubview:copyBtn];
    
    // 滑動邀請鏈接
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(linkLabel2.frame), 0, UI_View_Width - 60 - btnW - marginX * 2, cellH)];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    [linkView2 addSubview:scrollView];
    
    UITextView *link = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, UI_View_Width - 60 - btnW - marginX * 2, 50)];
    link.text = @"wwww.ujfwegertkyluiopafsdfghnlrjkliop[sdfghjklertyui測試測試滾動測試測試滾動測試測試滾動測試測試滾動測試測試滾動";
    link.textColor = [UIColor grayColor];
    link.textContainer.maximumNumberOfLines = 1;
    link.scrollEnabled = YES;//是否可以拖動
    link.editable = NO;//禁止編輯
    [scrollView addSubview:link];
    scrollView.contentSize = CGSizeMake(CGRectGetWidth(link.bounds), 50);
    self.link = link;
}

 

2、在scrollView里貼上 UITextView,用於上下滑動展示完整數據

#pragma mark - 按鈕點擊事件操作

/**
 * 復制鏈接
 */
- (void)copylinkBtnClick {
    
    NSLog(@"復制內容: %@", self.link.text);
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.link.text;
}

三、壓縮文件截圖及運行效果

1、壓縮文件截圖

F8B77280-9FC3-4A92-804B-9E595EB6E6D4.png

 

2、當顯示過多則滾動顯示的運行時的截圖

運行效果.gif

四、其他補充

界面性問題可以根據自己項目需求調整即可, 具體可參考代碼, 項目能夠直接運行!

 

注:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM