我有一个UILabel,但我怎样才能让用户选择它的一部分文本。我不希望用户能够编辑文本,也不希望标签/文本框具有边框。允许用户从UILabel中选择文本来复制
回答
这是不可能与UILabel
。
您应该使用UITextField
。只需使用textFieldShouldBeginEditing
委托方法停用编辑。
但是,这将有3D边界不是吗? – 2010-11-03 20:01:25
几周前我用过了UITextField,我记得没有边框(它是在xib中创建的)。如果你的UITextField有一个边框,那么只需阅读文档以找出如何禁用边框。 – Yuras 2010-11-03 20:06:56
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html [textField setBorderStyle:UITextBorderStyleNone] – Yuras 2010-11-03 20:08:06
您可以使用创建UITextView并将其.editable
设置为NO。然后你有一个文本视图,其中(1)用户不能编辑(2)没有边界,(3)用户可以从中选择文本。
一个穷人的复制和粘贴版本,如果你不能,或不需要使用文本视图,将添加一个手势识别器到标签,然后只是将整个文本复制到粘贴板。除非你使用UITextView
请确保你让用户知道它已被复制,并且你同时支持单击操作和长按操作,因为它会让用户尝试突出显示部分文字。这是一个有点示例代码,让你开始:
注册您的标签上的手势识别器,当你创建:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textPressed:)];
[myLabel addGestureRecognizer:tap];
[myLabel addGestureRecognizer:longPress];
[myLabel setUserInteractionEnabled:YES];
接下来处理手势:
- (void) textPressed:(UILongPressGestureRecognizer *) gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
[gestureRecognizer.view isKindOfClass:[UILabel class]]) {
UILabel *someLabel = (UILabel *)gestureRecognizer.view;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:someLabel.text];
...
//let the user know you copied the text to the pasteboard and they can no paste it somewhere else
...
}
}
- (void) textTapped:(UITapGestureRecognizer *) gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
[gestureRecognizer.view isKindOfClass:[UILabel class]]) {
UILabel *someLabel = (UILabel *)gestureRecognizer.view;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:someLabel.text];
...
//let the user know you copied the text to the pasteboard and they can no paste it somewhere else
...
}
}
不错的答案,但你应该添加一行代码:myLabel.userInteractionEnabled = YES; – Ilario 2016-03-08 09:45:32
- 1. 允许用户从表中选择
- 2. Rails复选框 - 允许用户选择本周的日期
- 3. 允许在复选框限制脚本中的最小选择
- 4. WPF文本块允许选择文本
- 5. CSS用户选择:禁用空格选择,但允许选择文本
- 6. 允许用户选择并复制引导按钮标签
- 7. 如何在使用cufon/sifr时允许用户选择文本?
- 8. 制作一个允许用户选择文件的对话框
- 9. 允许用户从下拉列表中选择选项
- 10. 允许用户选择链接文本的一部分
- 11. ng-file-upload - Android不允许用户选择本地文件
- 12. 强制网站允许选择文字?
- 13. 掩码文本,但仍允许用户复制它
- 14. 引导选择允许文本编辑
- 15. XamGrid不允许选择空文本
- 16. 允许用户选择一个目录
- 17. 允许用户选择输入
- 18. 允许用户订购选择清单?
- 19. 跟踪用户选择[允许/不]
- 20. 允许用户复制图像
- 21. 可能允许用户只选择一个复选框(引导复选框-x)?
- 22. 需要允许用户从sqllite数据库中选择颜色
- 23. 允许用户从个性化屏幕中选择背景
- 24. 允许用户从动态SQL中进行选择
- 25. 允许用户在Lotus Notes中选择文件夹路径
- 26. 方法来允许用户选择多个项目 - ASP.Net MVC
- 27. 允许访问Azure存储节点来选择用户?
- 28. 控制不允许选择时,文本框填充
- 29. 允许用户选择保存文件的文件路径
- 30. 在iPhone中复制UILabel中的文本
使用https: //github.com/hoteltonight/HTCopyableLabel – 2014-05-12 14:50:41