“少见"在可扩展标签 iOS 中
我正在使用第三方库 ExpandableLabel 来实现一个见更多功能.我正在寻找仅包含标签中而不是按钮中的文本的快速解决方案,因此这非常有效.在IB中添加库并更改标签类型后,我只需要几行代码:
I'm using the third party library ExpandableLabel to implement a see more feature. I am looking for swift only solutions that include the text in the label rather than in the button so this works perfectly. After adding the library and changing label type in IB I only need a few lines of code :
@IBOutlet weak var myLabel: ExpandableLabel!
myLabel = 3
myLabel = true
然而,在完全扩展后,我无法弄清楚如何实现少见".我添加了委托方法:
I can't however figure out how to implement "see less" after it has been expanded fully. I added the delegate method :
ExpandableLabelDelegate
和函数://MARK: ExpandableLabel 委托
and functions: // MARK: ExpandableLabel Delegate
func willExpandLabel(_ label: ExpandableLabel) {
}
func didExpandLabel(_ label: ExpandableLabel) {
}
func willCollapseLabel(_ label: ExpandableLabel) {
}
func didCollapseLabel(_ label: ExpandableLabel) {
}
func shouldCollapseLabel(_ label: ExpandableLabel) -> Bool {
return true
}
试图控制这个过程,但仍然很挣扎.有没有其他人设法做到这一点?如果是这样,请你能帮我在这里...
to try and gain control of the process but have still struggled. Has anyone else managed to get this right? If so please can you help me out here...
根据我的个人经验,我注意到 expandedAttributedLink
仅在您将其设置在实际标签文本之前才有效.
Based on my personal experience, I noticed that the expandedAttributedLink
works only if you set it before the actual label text.
infoLabel.setLessLinkWith(lessLink: "less", attributes: [NSFontAttributeName: boldItalicFont], position: nil)
infoLabel.text = viewModel.description
infoLabel.collapsedAttributedLink = NSAttributedString(string: "more", attributes: [NSFontAttributeName: boldItalicFont]);
infoLabel.ellipsis = NSAttributedString(string: "...")
infoLabel.collapsed = true
我能够通过查看 源文件.
I was able to identify this behaviour by looking at the setter of the text
property inside the source file.