【发布时间】:2016-10-11 21:48:33
【问题描述】:
我正在尝试增加标签内文本的行高。 我找到了这个代码示例:https://github.com/NativeScript/NativeScript/issues/1664#issuecomment-218977478
这应该可以解决问题,现在我正在尝试实现它。
这是我目前所拥有的:
xml 文件(从 tabView 加载):
<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事件触发的函数:
function onItemLoading(args) {
page = args.object;
var myLabel = page.getViewById("textLabel");
labelLineHeight(myLabel)
}
当然还有上面链接的 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')
任何帮助将不胜感激:)
【问题讨论】: