【问题标题】:Qml: use Text's elide property with textFormat: RichTextQml:使用 Text 的 elide 属性和 textFormat:RichText
【发布时间】:2015-04-28 23:31:51
【问题描述】:

我有一个带有转义 HTML 字符的 RSS 提要,我想在 Text 组件中显示它,我用 elide: Text.ElideRightwrapMode: text.WordWrap 修剪多余的内容。

虽然这对纯文本非常有效,但当我使用 textFormat: Text.RichText 时,修剪不起作用。

如何使修剪工作,或者,如果不可能,在将 HTML 绑定到文本组件之前轻松编码它?

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    确实Text 不支持elideText.RichText

    Qt 错误跟踪器上有一个bug open,在第一次回复后有一个可能的解决方案,我复制并粘贴到这里以便于阅读:

    TextEdit {
        property string htmlText: "<b>"+workingText.text+"</b>"
        text: htmlText
        width: parent.width
        onHtmlTextChanged: {elide();}
        onWidthChanged: elide();//Yes, this will be slow for dynamic resizing and should probably be turned off during animations
        function elide(){//Also, width has to be set, just like elide, or it screws up
            text = realText;
            var end = richText.positionAt(width - 28,0);//28 is width of ellipsis
            if(end != realText.length - 7)//Note that the tags need to be taken care of specially.
            text = realText.substr(0,end + 3) + '…' + '</b>';//3 is + <b>
        }
        font.pixelSize: 22
    }
    

    【讨论】:

      【解决方案2】:

      我想出了一个简单的解决方法,在我的情况下效果很好,其中 Text 组件的宽度和高度是固定的,即手动修剪字符串。最终结果在一定程度上取决于您的字体的等宽。

      text:  {
          var name = "my very very long text string with HTML markup&nbsp;that goes on forever and ever and ever"
          var text = name.substring(0,45)
          if (name.length > 45) text += "..."
              return text
      }
      

      【讨论】:

      • 实际上,这种方法有一个问题:HTML 实体计入它们的宽度。 &复制;长度为 6 个字符,但在您的文本中仅占用 1 个字符。如果一个字符串中有 3 个实体,您会丢失 18-20 个字符以显示 3 个字符
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多