UILabel中的NSAttributedString尾部截断 [英] NSAttributedString tail truncation in UILabel

查看:430
本文介绍了UILabel中的NSAttributedString尾部截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ContextLabel 来解析@,#和URL.这是我发现的最佳解决方案,可正确调整尺寸且不影响性能.它首先解析输入中的字符串,然后将其转换为NSAttributedString,然后将其分配给UILabelattributedText属性.一切都按预期工作,除了尾部截断-这是非常不正确的(请参见下图)

I'm using ContextLabel to parse @ , # and URL's. This is the best solution i found, cause it sizes correctly and dont affect performance. It firstly parses string at input and than converts it to NSAttributedString and after this assigns it to attributedText property of UILabel. Everything works as expected, except tail truncation - it's very incorrect ( see pic below )

我应该从哪里开始挖掘-属性字符串的属性是否错误?还是标签布局问题?谢谢!

Where shall i start digging - is it wrong attributes on attributed string? Or label layout issue? Thanks!

推荐答案

我遇到了此问题,并通过添加NSParagraphStyle指定所需的换行模式来解决此问题:

I had this problem and fixed it by adding a NSParagraphStyle specifying the desired line break mode:

    //assuming myString is an NSMutableAttributedString
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byTruncatingTail

    let range = NSRange(location: 0, length: myString.mutableString.length)
    myString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)

请参见 NSMutableAttributedString的自动换行以获取更多参考.

See Word wrap for NSMutableAttributedString for further reference.

这篇关于UILabel中的NSAttributedString尾部截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆