在NativeScript中创建可伸缩标签
我找到了一篇文章,解释了如何为Ios创建它。 但在Android中如何做到这一点 这就是我所尝试的:在NativeScript中创建可伸缩标签,nativescript,nativescript-vue,Nativescript,Nativescript Vue,我找到了一篇文章,解释了如何为Ios创建它。 但在Android中如何做到这一点 这就是我所尝试的: import {Label} from "tns-core-modules/ui/label"; function ScalingLabel() { this.myLabel = new Label(); let TextViewCompat; if (androidx) TextViewCompat = androidx.core.widget.Te
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 warn]: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>
我有一个后续问题。我玩它。但我注意到,当我不设置高度时,在完全看到之前,我实际上不会调整大小。第一个固定高度为12,第二个没有高度。问题是,当我有一个固定高度为12的较小文本时,很难看到,因为它不能更大。始终尝试将高度设置为至少比字体大小多10个点。感谢您的评论,没有更改任何内容。上述示例在我这方面非常有效。如果您有问题,请分享您的测试设备的示例和详细信息。