ios3怎么取消长按弹出菜单_UITextView长按弹出UIMenuController问题

iOS 上线应用被拒 反馈如下:Dear developer,

We have discovered one or more issues with your recent delivery for "紫色医疗医生版-医师执业助手第一平台.电话答题随诊访病历夹". To process your delivery, the following issues must be corrected:

Non-public API usage:

The app references non-public selectors in *****: _define:, _promptForReplace:, _share:, _transliterateChinese:

If method names in your source code match the private Apple APIs listed above, altering your method names will help prevent this app from being flagged in future submissions. In addition, note that one or more of the above APIs may be located in a static library that was included with your app. If so, they must be removed.

If you think this message was sent in error and that you have only used Apple-published APIs in accordance with the guidelines, send the app's nine-digit Apple ID, along with detailed information about why you believe the above APIs were incorrectly flagged, toappreview@apple.com. For further information, visit theTechnical Support Informationpage.

Once these issues have been corrected, you can then redeliver the corrected binary.

Regards,

The App Store team

大概意思就是我们用了 苹果的私有 api  _define:, _promptForReplace:, _share:, _transliterateChinese:的方法

列举选择项

cut:  copy:  select:  selectAll:  paste:  delete:  _promptForReplace:  _transliterateChinese:  _showTextStyleOptions:  _define:  _addShortcut:  _accessibilitySpeak:  _accessibilitySpeakLanguageSelection:  _accessibilityPauseSpeaking:  makeTextWritingDirectionRightToLeft: makeTextWritingDirectionLeftToRight:

其中下边这些是可以访问的公开方法- (void)cut:(nullableid)senderNS_AVAILABLE_IOS(3_0);

- (void)copy:(nullableid)senderNS_AVAILABLE_IOS(3_0);

- (void)paste:(nullableid)senderNS_AVAILABLE_IOS(3_0);

- (void)select:(nullableid)senderNS_AVAILABLE_IOS(3_0);

- (void)selectAll:(nullableid)senderNS_AVAILABLE_IOS(3_0);

- (void)delete:(nullableid)senderNS_AVAILABLE_IOS(3_2);

- (void)makeTextWritingDirectionLeftToRight:(nullableid)senderNS_AVAILABLE_IOS(5_0);

- (void)makeTextWritingDirectionRightToLeft:(nullableid)senderNS_AVAILABLE_IOS(5_0);

现在有个需求如下图:添加一个"添加为模版"

第一种代码

代码如下:自定义了一个PHTTextView 继承UITextView 中加入一下代码-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

if(action ==@selector(copy:) ||

action ==@selector(selectAll:)||

action ==@selector(cut:)||

action ==@selector(select:)||

action ==@selector(paste:)) {

return[supercanPerformAction:actionwithSender:sender];//

}

returnNO;

}

在使用PHTTextView的controller中添加如下代码UIMenuItem*menuItem = [[UIMenuItemalloc]initWithTitle:@"添加为模板"action:@selector(addToTemplate:)];

UIMenuController*menu = [UIMenuControllersharedMenuController];

[menusetMenuItems:[NSArrayarrayWithObjects:menuItem,nil]];

实现效果上图那样. 点击"全选"之后   全选按钮 就没有了

第二种代码

如果把代码改成 如下-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

if(action ==@selector(copy:) ||

action ==@selector(selectAll:)||

action ==@selector(cut:)||

action ==@selector(select:)||

action ==@selector(paste:)) {

returnYES; *--让满足条件的  都显示出来--*

}

returnNO;

}

点击全选之后  效果如下

其实这时候 就能 看出来 apple 的这个方式是给我们优化的  [super canPerformAction:action withSender:sender]; 因为没有全部选择之前 进行"剪切""拷贝"是没有任何意义的.

如果固定 全部写成 YES 他会把你所有 想要的展示出来.

第三种代码

把 PHTTextView 中的 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender方法改为第一种代码

让后在viewcontroller中添加了 如下 代码-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

if(action ==@selector(addToTemplate:)) {//添加为模板方法

if(self.contentView.selectedRange.length>0) {

returnYES;

}else{

returnNO;

}

return[supercanPerformAction:actionwithSender:sender];

}

效果如下

点击"全选"或者"选择"之后 才会出来 "添加为模板" 是因为 这句话 if(self.contentView.selectedRange.length>0) {returnYES;}else{returnNO;} 如果textview 又被选择的文字 才会 返回 yes 否则no

第四种代码

把PHTTextView中的-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 方法全部注释

并且修改ViewController 方法 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

if(action ==@selector(addToTemplate:)) {//添加为模板方法

if(self.contentView.selectedRange.length>0) {

returnYES;

}else{

returnNO;

}

}elseif(action ==@selector(copy:) ||

action ==@selector(selectAll:)||

action ==@selector(cut:)||

action ==@selector(select:)

){

BOOLisAppear = [super canPerformAction:actionwithSender:sender];

return isAppear;

}

//BOOLisAppear = [super canPerformAction:actionwithSender:sender];

return NO;

}

效果如下

点击"全选"

会出来很多不想要的事件  我也不知道为什么........... 求解

建议使用第一种和第三种  第三种 是优化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下代码实现 UITextView 按复制粘贴的功能: ```swift // 设置允许复制和粘贴 textView.isSelectable = true textView.allowsEditingTextAttributes = true textView.textContainerInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0) // 添加按手势识别器 let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:))) textView.addGestureRecognizer(longPressGesture) // 按手势识别器的响应函数 @objc func longPress(_ gestureRecognizer: UIGestureRecognizer) { // 获取按的位置 let point = gestureRecognizer.location(in: textView) // 获取按位置所在的字符索引 let characterIndex = textView.layoutManager.characterIndex(for: point, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil) // 判断按位置是否在文本范围内 if characterIndex < textView.textStorage.length { // 获取按位置所在的单词 let range = textView.tokenizer.rangeEnclosingPosition(TextViewPosition(document: textView.textStorage), with: .word, inDirection: UITextDirection(rawValue: 1)) let word = textView.text(in: range!) // 弹菜单 let menuController = UIMenuController.shared if !menuController.isMenuVisible { let copyMenuItem = UIMenuItem(title: "复制", action: #selector(copyText(_:))) let pasteMenuItem = UIMenuItem(title: "粘贴", action: #selector(pasteText(_:))) menuController.menuItems = [copyMenuItem, pasteMenuItem] menuController.setTargetRect(CGRect(x: point.x, y: point.y, width: 0, height: 0), in: textView) menuController.setMenuVisible(true, animated: true) } } } // 复制文本 @objc func copyText(_ sender: Any) { UIPasteboard.general.string = textView.text(in: textView.selectedTextRange!) } // 粘贴文本 @objc func pasteText(_ sender: Any) { if let pasteString = UIPasteboard.general.string { let selectedRange = textView.selectedRange let mutableAttributedString = NSMutableAttributedString(attributedString: textView.attributedText) mutableAttributedString.replaceCharacters(in: selectedRange, with: NSAttributedString(string: pasteString)) textView.attributedText = mutableAttributedString } } ``` 这里使用了 `UITextView` 的 `UITextInputTokenizer` 对象来获取按位置所在的单词,并通过 `UIMenuController` 弹菜单来实现复制和粘贴的功能。同时,需要在 `UITextView` 中设置 `isSelectable` 和 `allowsEditingTextAttributes` 属性为 `true`,并添加按手势识别器来启用按功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值