英文:
Change the wording of VNdocumentCameraViewController
问题
我想通过显示VNDocumentCameraViewController来更新视图中显示的文字。
例如,
在这张图片中,我想要更改:
-
Position the document in view -> 将文档定位在视图中 -> 你好,世界
-
Cancel -> 取消 -> 停止
-
Auto -> 自动 -> 删除按钮
我不确定VNDocumentCameraViewController具有哪些属性,因此无法在子类中覆盖这些按钮标题的值。
如何覆盖这些按钮标题为不同的值呢?
英文:
I'd like to update the wordings shown in the view by presenting the VNDocumentCameraViewController.
For example,
In this picture, I'd like to change,
-
Position the document in view -> Hello world
-
Cancel -> Stop
-
Auto -> Delete the button
I'm not sure what attributes VNDocumentCameraViewController has, so cannot override the values in the child class.
How is it possible to override these button title to a different value?
答案1
得分: 3
以下是翻译好的内容:
简短回答是,如果你要发布到应用商店,是不允许这样做的;Apple提供了UI,而且因为他们没有提供自定义的方法,你应该将它保持原样。更详细的回答是,你可以遍历视图层次结构,找出哪些标签包含哪些文本,然后可以简单地更改这些UILabel上的文本。有很多原因不建议这样做:
- 这可能会导致你的应用被应用商店拒绝。
- 实现可能会在不同版本之间发生变化,破坏你的应用程序。
- 文本是本地化的,因此你的更改在其他语言中不起作用。
在提醒之后,以下是用于执行深度优先搜索以查找与特定条件匹配的视图的代码,例如,是否为带有特定文本的UILabel。
import SwiftUI
import PlaygroundSupport
let a = UIView()
let b = UIView()
let c = UIView()
let l = UILabel()
let z = UILabel()
a.addSubview(b)
a.addSubview(c)
b.addSubview(z)
c.addSubview(l)
l.text = "hello"
extension UIView {
func child(matching predicate: (UIView) throws -> Bool) rethrows -> UIView? {
for child in subviews {
if try predicate(child) {
return child
}
if let found = try child.child(matching: predicate) {
return found
}
}
return nil
}
}
print(l === a.child(matching: {($0 as? UILabel)?.text == "hello"}))
希望这对你有所帮助。
英文:
The short answer is that you are not allowed to do this if you re releasing to the App Store; Apple provides the UI and since they don't provide a way to customize it you are supposed to leave it as is. The longer answer is that you can walk the view hierarchy and figure out which labels contain which text and then you can simply change the text on these UILabels. Its bad to do this for a lot of reasons:
- It can get you rejected from the app store
- The implementation can change between version, breaking your app
- The text is localized so your changes will not work in other languages
With that warning, here is code to do a DFS search to find a view that matches a certain predicate, for instance, being a UILabel will some specific text.
import SwiftUI
import PlaygroundSupport
let a = UIView()
let b = UIView()
let c = UIView()
let l = UILabel()
let z = UILabel()
a.addSubview(b)
a.addSubview(c)
b.addSubview(z)
c.addSubview(l)
l.text = "hello"
extension UIView {
func child(matching predicate: (UIView) throws -> Bool) rethrows -> UIView? {
for child in subviews {
if try predicate(child) {
return child
}
if let found = try child.child(matching: predicate) {
return found
}
}
return nil
}
}
print(l === a.child(matching: {($0 as? UILabel)?.text == "hello"}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论