nativescript - 在 NativeScript 中创建可缩放标签
问题描述
我发现了一篇文章如何解释它为 Ios 创建它。 http://nuvious.com/Blog/2015/5/9/creating-a-scalable-label-in-nativescript
但是如何在Android中做到这一点。
这就是我尝试过的:
import {Label} from "tns-core-modules/ui/label";
function ScalingLabel() {
this.myLabel = new Label();
let TextViewCompat;
if (androidx)
TextViewCompat = androidx.core.widget.TextViewCompat;
else
TextViewCompat = android.support.v4.widget.TextViewCompat;
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this.myLabel, 8, 100, 1, 2);
}
ScalingLabel.prototype = new Label();
exports.ScalingLabel = ScalingLabel;
Vue.registerElement('ScalingLabel', ScalingLabel);
我收到以下错误:
[Vue 警告]:v-on 处理程序中的错误:“TypeError:无法加载视图:nativescalinglabel。错误:无法将对象转换为 Landroid/widget/TextView;在索引 0”
解决方案
您不应该创建一个实例,而是扩展原始类。
class ScalingLabel extends Label {
initNativeView() {
super.initNativeView();
if (this.android) {
androidx.core.widget.TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this.nativeViewProtected, 10, 100, 1, android.util.TypedValue.COMPLEX_UNIT_SP);
}
}
}
Vue.registerElement('ScalingLabel', () => ScalingLabel);
然后在你的组件中尝试,
<template>
<Page class="page">
<ActionBar title="Home" class="action-bar" />
<ScrollView>
<StackLayout class="form">
<TextField class="m-15 input input-border" v-model="message">
</TextField>
<Label :text="message" class="m-5 h3" />
<ScalingLabel :text="message" class="m-5 h3" height="30"
width="100%" textWrap="true" />
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
export default {
data() {
return {
message: ""
};
}
};
</script>
推荐阅读
- javascript - 创建对象和对象数组
- node.js - Spectron:如何测试 Electron `shell.openExternal('some url')`
- azure - 在 Azure Logic App 中,如何在循环条件第一次迭代失败的情况下中断循环
- rust - 如何在字符串字符的反向迭代器中从头开始获取索引?
- vue.js - vue:转义和渲染 HTML 字符串?
- python - Python 将 2000 多个 csv 列合并为一列
- html - 表格的第一列尽可能宽,其他列等宽
- c++ - 错误:没有匹配函数调用 'std::vector
::向量( )' - python-3.x - 如何拆分网络抓取的列名?
- javascript - 从第二个 iframe 获取内容