如何修改UITextView中UIResponderStandardEditActions的显示语言?
如何修改UITextView中UIResponderStandardEditActions的显示语言?
嘿,我完全懂你这个困扰!明明设备已经切换成中文了,UITextView弹出的那些标准编辑菜单(比如复制、剪切)却还是顽固显示英文,找遍谷歌和苹果文档都没头绪,确实挺头疼的。
我之前也帮朋友解决过类似的问题,给你几个可行的思路:
先排查项目本地化配置
有时候不是系统的问题,而是你的项目没正确开启中文本地化支持:
- 打开Xcode项目,进入项目设置的「Info」标签页,找到「Localizations」部分,确认已经添加了「Chinese (Simplified)」,并且勾选了对应的资源文件(比如Info.plist)。
- 检查Info.plist里的
CFBundleDevelopmentRegion键,值是不是设为zh-Hans或者zh_CN,这个项决定了App的默认本地化区域。 - 确保你的项目里有中文的
Localizable.strings文件,哪怕里面暂时没内容,也能让系统优先读取设备的本地化语言。
自定义编辑菜单标题(最稳妥的方案)
如果排查后还是没解决,那可以通过重写UITextView的子类,手动替换菜单的标题。这种方法不受系统配置影响,完全可控:
Objective-C版本
@interface CustomTextView : UITextView @end @implementation CustomTextView - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { // 保留系统默认的编辑权限逻辑 BOOL canPerform = [super canPerformAction:action withSender:sender]; UIMenuController *menuController = [UIMenuController sharedMenuController]; NSMutableArray *updatedMenuItems = [menuController.menuItems mutableCopy] ?: [NSMutableArray array]; // 先移除原有对应动作的菜单项 [updatedMenuItems removeObjectPassingTest:^BOOL(UIMenuItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { return obj.action == action; }]; // 根据动作类型添加自定义标题的项 if (action == @selector(copy:)) { [updatedMenuItems addObject:[[UIMenuItem alloc] initWithTitle:@"复制" action:action]]; } else if (action == @selector(cut:)) { [updatedMenuItems addObject:[[UIMenuItem alloc] initWithTitle:@"剪切" action:action]]; } else if (action == @selector(paste:)) { [updatedMenuItems addObject:[[UIMenuItem alloc] initWithTitle:@"粘贴" action:action]]; } else if (action == @selector(selectAll:)) { [updatedMenuItems addObject:[[UIMenuItem alloc] initWithTitle:@"全选" action:action]]; } menuController.menuItems = updatedMenuItems; return canPerform; } @end
Swift版本
class CustomTextView: UITextView { override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { let canPerform = super.canPerformAction(action, withSender: sender) guard let menuController = UIMenuController.shared as? UIMenuController else { return canPerform } var updatedMenuItems = menuController.menuItems ?? [] // 移除原有对应动作的菜单项 updatedMenuItems.removeAll { $0.action == action } // 添加自定义标题的菜单项 switch action { case #selector(copy(_:)): updatedMenuItems.append(UIMenuItem(title: "复制", action: action)) case #selector(cut(_:)): updatedMenuItems.append(UIMenuItem(title: "剪切", action: action)) case #selector(paste(_:)): updatedMenuItems.append(UIMenuItem(title: "粘贴", action: action)) case #selector(selectAll(_:)): updatedMenuItems.append(UIMenuItem(title: "全选", action: action)) default: break } menuController.menuItems = updatedMenuItems return canPerform } }
之后你只需要把项目里的UITextView替换成这个自定义子类,就能看到中文的编辑菜单了。如果需要支持多语言,把硬写的中文标题换成NSLocalizedString(@"copy_title", @""),然后在对应语言的strings文件里配置即可。
额外注意
如果你的项目里用到了第三方输入框或者自定义键盘,可能会覆盖系统的菜单逻辑,这时候需要检查第三方库的设置,看看有没有强制设置语言的代码。
备注:内容来源于stack exchange,提问作者stephen
相关产品推荐
相关产品推荐

