我想将自定义字体设置为UIButton
. 我需要以编程方式设置它,因为否则不会应用字体。问题是单击后它会变回内置字体。我究竟做错了什么?
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var eyeButton: UIButton!
@IBOutlet weak var loginButton: UIButton!
var iconClick = true
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
loginButton.titleLabel?.font = UIFont(name: "Capitana-Bold", size: CGFloat(16))
}
@IBAction func iconAction(sender: AnyObject) {
if (iconClick == true) {
passwordTextField.isSecureTextEntry = false
eyeButton.setImage(UIImage(named: "eye-closed"), for: .normal)
} else {
passwordTextField.isSecureTextEntry = true
eyeButton.setImage(UIImage(named: "eye-open"), for: …
Run Code Online (Sandbox Code Playgroud) 我正在实现 ML Kit OCR 功能,有时,前几个值是错误的,只有在一段时间后相机才会稳定并产生正确的值。我不想删除前 X 值,因为我不知道该流将包含多少个元素。因此,最好的方法是使用某种条件,将当前元素与前一个元素进行比较,但不确定。
Kotlin Flow API 中是否有一个函数可以比较收集的值并仅收集至少出现 N 次的值?
private val _detectedValues = ConflatedBroadcastChannel<String>()
val detectedFlow = _detectedValues
.asFlow()
.map { it.replace(" ", "") }
.filter { it.checkRegex() }
.onEach {
Log.i(TAG, "detected: $it")
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用 Android 开发人员文档使用 CameraX 实现 MLKit 文本分析器。我的期望是分析器将在每一帧上运行,但它只运行一次,当预览初始化时。文本由 MLKit 分析,但只有在我旋转屏幕并重新启动片段时才会执行分析。为什么会发生这种情况?
在我使用的 XML 中<androidx.camera.view.PreviewView .../>
。
在片段中:
private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
private fun startCamera() {
context?.let { ctx ->
cameraProviderFuture = ProcessCameraProvider.getInstance(ctx)
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
bindPreview(cameraProvider)
}, ContextCompat.getMainExecutor(ctx))
}
}
private fun bindPreview(cameraProvider: ProcessCameraProvider) {
val preview: Preview = Preview.Builder()
.build()
val cameraSelector: CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
val analyzer = ImageAnalysis.Builder()
.setTargetResolution(Size(1280, 720))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build().apply {
setAnalyzer(Executors.newSingleThreadExecutor(), TextAnalyzer())
}
val camera = cameraProvider.bindToLifecycle(viewLifecycleOwner, cameraSelector, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Flows 测试 Kotlin 实现。我使用 Kotest 进行测试。此代码有效:
视图模型:
val detectedFlow = flow<String> {
emit("123")
delay(10L)
emit("123")
}
Run Code Online (Sandbox Code Playgroud)
测试:
class ScanViewModelTest : StringSpec({
"when the flow contains values they are emitted" {
val detectedString = "123"
val vm = ScanViewModel()
launch {
vm.detectedFlow.collect {
it shouldBe detectedString
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
但是,在真正的 ViewModel 中我需要向流添加值,所以我使用ConflatedBroadcastChannel
如下:
private val _detectedValues = ConflatedBroadcastChannel<String>()
val detectedFlow = _detectedValues.asFlow()
suspend fun sendDetectedValue(detectedString: String) {
_detectedValues.send(detectedString)
}
Run Code Online (Sandbox Code Playgroud)
然后在测试中我尝试:
"when the flow contains values they are emitted" …
Run Code Online (Sandbox Code Playgroud) kotlin kotlin-coroutines kotlin-flow kotest kotlin-coroutines-flow