0
我試圖增加標籤內部文本的行高度。 我發現這個代碼示例:https://github.com/NativeScript/NativeScript/issues/1664#issuecomment-218977478使用Nativescript更改標籤內文本的行高
這應該做的伎倆,現在我試圖實現它。
這是我到目前爲止有:
XML文件(從TabView的加載):
function onItemLoading(args) {
page = args.object;
var myLabel = page.getViewById("textLabel");
labelLineHeight(myLabel)
}
和:
<ListView id="news-feed" items="{{ news }}" loaded="onLoaded" itemLoading="onItemLoading" separatorColor="#f4f4f4">
<ListView.itemTemplate>
<GridLayout backgroundColor="#f4f4f4">
<StackLayout tap="openInWebview" class="news-card">
<Label text="{{ headline }}" textWrap="true" class="headline" />
<Label id="textLabel" text="{{ lead }}" textWrap="true" />
</StackLayout>
</GridLayout>
</ListView.itemTemplate>
</ListView>
由事件itemLoading所觸發的功能當然在上面鏈接的Github問題中描述的功能:
function labelLineHeight(nsLabel) {
// console.dump(nsLabel)
if(page.ios){
var label = nsLabel.ios;
var attributedString;
if(label.attributedText){
attributedString = label.attributedText;
}
else{
attributedString=NSMutableAttributedString.alloc().initWithString(label.text);
}
var paragraphStyle = NSMutableParagraphStyle.alloc().init();
paragraphStyle.lineSpacing = 5;
var range= NSMakeRange(0, label.text.length);
attributedString.addAttributeValueRange(NSParagraphStyleAttributeName, paragraphStyle, range);
label.attributedText = attributedString;
}
if(page.android){
var label = nsLabel.android;
//Default spacing is 20% of text size
//setLineSpacing(add,multiplyby);
label.setLineSpacing(12, 1);
}
}
和錯誤消息我得到:
[45842]: file:///app/mainTabs/tabNews/tabNews.js:40:28: JS ERROR TypeError: undefined is not an object (evaluating 'nsLabel.ios')
任何幫助,將不勝感激:)
感謝您的答案。 – Dailyrox
我最終手動建立了整個視圖。 – Dailyrox