apploft / expandablelabel Goto Github PK
View Code? Open in Web Editor NEWA simple UILabel subclass that shows a tappable link if the content doesn't fit the specified number of lines
License: MIT License
A simple UILabel subclass that shows a tappable link if the content doesn't fit the specified number of lines
License: MIT License
I have specified the detailTextLabel in a UITableViewCell to be an ExpandableLabel. The layout of the expandable label is not working very well. When the table is first loaded it shows only a few characters of the label text and no "...More" link. When scrolling down a little so the cell is no longer visible and then scrolling back up the expandable label is shown correctly. I'm pulling my hair out to figure out how I can trigger the expandable label to do its layout correctly the first time. It can also act strange... sometimes putting the ...More link too early in the text. And then after scrolling a few times more it can all of a sudden be correct again. I'm calling cell.layoutIfNeeded() after the text is set but it doesn't help.
Hi,
How can i collapse a cell on selection of another cell.
I'm getting an Apple Match-O Linker error
I remove & re-installed the ExpandableLabel Pod, I cleaned my project but it's still not working.
Are there any plans of bringing this lib to Carthage?
Close button is not Working and one more Thing in delegate method for ...More I want to reload a particular cell not the entire tableView
Read More added in the first line , but my condition was to add it after the 3rd line.
This happens after you only reload 1 cell in the table view , then the next cell after that automatically shows "read more" for 1 line text only.
Does this project support swift 4? If not, when do you think you will have a version for swift 4 available?
Hello
I want this code with objective c if you possible else suggest me other library
The expandableLabel's textAlignment is always .left without referring textAlignment property.
I've set it to .Right or .Natural, but the label shows the text always as .Left
Please help me solve this issue.
Strangely it does not work in some cells. It shows the text, and it's truncated correctly, but there is no "More" to click to expand the text. It's get more strange since the problem gets fixed when I go to another view (page) and then back to the table.
The text gets truncated to one line, and only one character if numberOfLines = 1
Label crashes when setting strings like this:
"╭┈┈┈┈╯ ╰┈┈┈╮\n\n ╰┳┳╯ ╰┳┳╯\n 💧 💧\n\n "
I am having issues implementing this in my VC in iOS 11. Do you know if there are any issues when using this project in iOS 11? I have the expandable label in a scrollview not a tableview like in the demo.
please add the version for the swift 3.2 so we can use in lower version to.
Hi There,
Thanks for creating this awesome control !
I am facing a wired issue in this. If I set custom font in expandable label its is not working it is only working in system font.
Can you plz help me in this?
Thanks in again
Hoping for positive and the quick reply.
Read more button appear after dequeue in UItableView, initially it doesn't show.
If an Expandable Label is in a tableview cell and a user taps the Expandable Label, "didselectrow" at index path is not called. It does not allow us to select a cell if a user taps on the Expandable Label within that cell.
Hi ,
Expandable Label is not compiling with Xcode 10.2 which has Swift 5 Version.
Please update this to Swift 5.
The functions willExpandLabel, didExpandLabel, etc are not being called.
I added ExpandableLabelDelegate to my controller class and label.delegate = self on the ExpandableLabel.
Any solution?
Noticed this when I constructed my components programmatically - the label doesn't handle size changes.
Now on each size change (width change as it were) the calculations need to be redone
I would create a PR but I think it would be best to change the component for that - the main point is that the original text needs to be stored somewhere, otherwise we can't redo the calculations.
I made the following changes to make it work
USAGE CHANGE: set the fullText var instead of either text or attributedText
expandedLabel.fullText = NSAttributedString(string: "Something")
Add the following method / var to class ExpandedLabel
....
open var fullText: NSAttributedString?
....
override open func layoutSubviews() {
super.layoutSubviews()
self.attributedText = fullText
}
This way, I can create the component programatically and lay it out before it's ever shown on screen and it will work
Also, if the layout changes for some other reason, it's also going to work.
Tested, works fine so far
If I press to label's link the cell's content jumps.
"Read more" button appear in the middle of the line and after a couple of reloadData its appears at the correct position (toward the end of the line). I tried layoutIfNeeded and settign the width of the label before setting the text but nothing is working.
Any ideas?
File ExpandableLabel.swift
line 140
open override var text: String? {
set(text) {
if let text = text {
expandedText = getExpandedTextForText(text, link: expandedAttributedLink)?.copyWithAddedFontAttribute(font)
self.attributedText = NSAttributedString(string: text)
} else {
self.attributedText = nil
}
}
get {
return self.attributedText?.string
}
}
Will crash if set empty string - ""
I am getting an error "is inaccessible due to internal protection level" when trying to access the method setLessLinkWith. Did you not make this public?
When I add ExpandedLabel programatically as sub view then it does not detect proper touch coordinate.
label = ExpandableLabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: CGFloat.leastNormalMagnitude))
label?.collapsed = true
label?.shouldCollapse = true
label?.textReplacementType = .character
label?.setLessLinkWith(lessLink: "Show Less", attributes: [.foregroundColor:UIColor.red], position: .natural)
label?.collapsedAttributedLink = NSAttributedString(string: "Show More", attributes: [.font: UIFont.boldSystemFont(ofSize: (label?.font.pointSize)!)])
label?.numberOfLines = 1
view.addSubview(label!)
label?.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: label!, attribute: NSLayoutAttribute.leadingMargin, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leadingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: label!, attribute: NSLayoutAttribute.trailingMargin, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.trailingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: label!, attribute: NSLayoutAttribute.topMargin, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.topMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: label!, attribute: NSLayoutAttribute.bottomMargin, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, constant: 0).isActive = true
label?.text = "Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test "`
When I use either of these two lines of code, the field does not collapse. This is just a normal VC not a tableview cell. It will expand but it will not collapse.
descriptionLabel.expandedAttributedLink = NSAttributedString(string: "show less",attributes: [ NSForegroundColorAttributeName: UIColor( red: 42/255, green: 91/255, blue:140/255, alpha: 1 ), NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
descriptionLabel.setLessLinkWith(lessLink: "show less", attributes: [NSForegroundColorAttributeName: UIColor( red: 42/255, green: 91/255, blue:140/255, alpha: 1 ), NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)], position: nil)
I know the demo is working, but in my code, when I write mCell.expandablelabel.delegate=self, and add delegate functions at the end, it seems it doesn't fire the delegate function when I tap on "read more". I tested it by print("test") in the methods.
PS: I converted my app from Swift 3.2 to 4 to work with this pod. My app worked with Swift 3 version of this pod before.
I want to use in the scrollView
Hi
Read more does not comes at first load, after scrolling it appears this problem only occurs in iPhone 5s and below.
Also the last item get automatically collapsed on expanding it.
How can i solve this pls help.
Thanks
Hey,
Any plan to support Swift 3.0?
Bests
@IBOutlet weak var label: ExpandableLabel!
@IBOutlet weak var labelForChinese: ExpandableLabel!
override func viewDidLoad() {
super.viewDidLoad()
[self.label,self.labelForChinese].forEach { (xxlable) in
xxlable?.numberOfLines = 3
xxlable?.collapsed = true
xxlable?.backgroundColor = UIColor.lightGray
xxlable?.collapsedAttributedLink = NSAttributedString(string: "[Show More]")
xxlable?.expandedAttributedLink = NSAttributedString(string: "[Show Less]")
xxlable?.shouldCollapse = true
}
self.label.text = "On third line our text need be collapsed because we have ordinary text, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
self.labelForChinese.text = "中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文文中文中文中文"
}
a couple of days ago, I sent a PR that upgrades the code to Swift 4.2 and it was merged by @tinora (thanks!). Can you guys release this? I need to pull it into my lib :-)
Hi,
collapsedAttributedLink is not shown when i set number of line for label to 1 and expandedAttributedLink is not showing always. On tab of label expand and shrink is working but collapsedAttributedLink and expandedAttributedLink text is not shown. I have tried the same demo from github in that also it is not displaying. I have tried setLessLinkWith also.
Hi there,
I'm setting my attributed string the following way:
let fontFamily = "font-family: \(fontName);"
let fontSize = "font-size: \(fontSize);"
let color = "color: \(htmlRGBColor);"
let htmlString = "<p style=\"\(fontFamily) \(fontSize) \(color)\">\(self)</p>"
let attributedString = try! NSMutableAttributedString(
data: htmlString.dataUsingEncoding(NSUTF16StringEncoding)!,
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
documentAttributes: nil)
if let paragraphStyle = aParagraphStyle {
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
}
For some reason, when I set the text alignement and the line spacing through the additional paragraph style attribute, bot the collapsedAttributedLink
and the ellipsis
are not displayed anymore.
I observed that getCollapsedTextForText()
consistently returns the expanded text (instead of the collapsed one) and I supsect it is because, in this case, linesForWidth(_ :)
always returns a a wrong array of CTLinesRef
, but I'm struggling to go forward in the debugging.
Any idea?
Thank you for your help!
UPDATE :
A workaround for the specific line-height
issue (one of the variables I set on my NSParagraphStyle
) is to set it via HTML styles.
However, I cannot set the text alignement using the same technique as the text-align
property is exclusively available with block tags and a bug in iOS (libxml?) prevents me from doing so as it adds an unwanted extra space at the bottom of the string (see this issue)
Hence, being able to add most of the attributes via the addAttributed:value:range
method would be really convenient 😄
I have assigned the animateview still the animation is not working.
I have just one expandableLabel and the 'see more" function works perfectly. Is it possible to add a "see less" function now that will return the label to its original state?
I use the ExpandableLabel in a tableview, when I press on MORE in one of the CELL then all my cells have the red CLOSE in them, even those that didn't need to expand
Hi,
I have text for label as (A \n B \n C) and i have number of line is 2 for collapsed state. It is showing just ...More for label
I want scroll position to stay where label is tapped.
Currently tableView scrolls up, when any of the cell label is tapped from bottom.
Hi,
Can you put up an example of bridging this with objective c code?
Thanks
When u will add text like :
text
text
\n
text
text
More link not appear in latestLine because code enumerates by words...
I fixed issue in my locall file and will contribute later.
There are @objc declarations missing for delegate property and for protocol declaration in the library, and therefore Objective-C doesn't see this property at all and can't be used within Obj-C project. Please include this fix in your next release (add @objc before the delegate property and before protocol). Would be a good idea to create sample project in Objective-C just to be sure everything written is Swift is accessible from objective-c. It might also be a good idea to make all those delegate methods optional, so developers can decide which one is needed, and not include all 4 just to use a single one in the project.
I got this weird issue. The second and so on is OK. Anyone else got this problem ?
How to increase gap between the two lines of text of label
Hello,
I was using ExpandableLabel
on a vertical StackView
with these configurations :
Fill
Fill
I just changed these ExpandableLabel
properties through storyboard :
On
bla bla bla
3
When I ran the app, the label shows nothing.
Do you have any suggestions?
Thanks!
A leak is being produced when an instance of ExpandableLabel is allocated on memory.
For instance, if an ExpandableLabel is added to a UITableViewCell, when using instruments one can perceive that such cells are causing memory issues as elements persist even after deallocation of cells.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.