ios - 如何知道何时从 Objective-C 中的特定文本字段复制文本?
问题描述
我有一些可编辑的文本字段。当用户选择其文本复制和粘贴菜单项时显示。
我想知道用户何时点击副本。因为我想更改复制的文本。
可能吗?
更多详细信息: 我想更改 3 个文本字段的副本。当复制其中一个文本字段时,我想将所有这 3 个文本字段文本连接到剪贴板。此页面中还有其他文本字段,但我不想为它们做任何事情。
解决方案
您可以实现该copy()
方法来“拦截”复制操作并修改放置在剪贴板上的内容。
最简单的方法可能是一个简单的子类UITextField
:
//
// MyTextField.h
//
// Created by Don Mag on 5/29/19.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyTextField : UITextField
@end
NS_ASSUME_NONNULL_END
和
//
// MyTextField.m
//
// Created by Don Mag on 5/29/19.
//
#import "MyTextField.h"
@implementation MyTextField
- (void)copy:(id)sender {
// debugging
NSLog(@"copy command selected");
// get the selected range
UITextRange *textRange = [self selectedTextRange];
// if text was selected
if (textRange) {
// get the selected text
NSString *selectedText = [self textInRange:textRange];
// change it how you want
NSString *modifiedText = [NSString stringWithFormat:@"Prepending to copied text: %@", selectedText];
// get the general pasteboard
UIPasteboard *pb = [UIPasteboard generalPasteboard];
// set the modified copied text to the pasteboard
pb.string = modifiedText;
}
}
@end
推荐阅读
- angular - Typescript - 在运行时检查类型并警告错误
- c# - 使用 linq 检查 IEnumerable 列表的计数
- r - 如何在 R 中分组并作为列表返回?
- angular - 如何在 SVG 路径中使用 routerLink?
- c++ - 具有许多派生类的可重用类的良好设计
- c# - VS2015 express下的WPF项目多和不同的构建
- mongodb - 在 mongodb 中设计评论/回复集合
- mysql - 将当前日期设置为 jDateChooser
- excel-formula - 如果 1 个标准是文本并且 2 个标准是谷歌表格中的颜色,则求和
- python - 遍历等阴影的 Pythonic 方式