“See Less” in ExpandableLabel iOS

2019-05-19 03:26发布

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 

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...

标签: ios swift swift3
1条回答
欢心
2楼-- · 2019-05-19 04:02

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.

查看更多
登录 后发表回答