【问题标题】:Change the line height of the text inside a label with Nativescript使用 Nativescript 更改标签内文本的行高
【发布时间】: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')

任何帮助将不胜感激:)

【问题讨论】:

    标签: javascript nativescript


    【解决方案1】:

    我注意到您为标签分配 id 的方式不正确。原因是这样,您将为每个具有相同 ID 的列表视图项拥有多个 ID。您应该在列表视图的项目模板中为您的标签动态生成不同的 ID(例如:通过在末尾为它们提供具有唯一索引的 id-name)。

    另一件事是您试图在 onItemLoading 中获取 ID。在父视图的 loaded 事件中执行此操作(在列表视图、父视图甚至是带有 onLoaded 回调的页面加载事件)。

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多