Read More/Less with Swift 3
- Create an outlet for height constraint of your
messageLabel
- Set top layout of your "Read more" button to
messageLabel
On clicking "Read more" button increase height constraint constant, on clicking "Read less" decrease height constraint constant.
@IBOutlet weak var btn: UIButton!
@IBOutlet weak var lblHeight: NSLayoutConstraint!
var isLabelAtMaxHeight = false
@IBAction func btnAction(_ sender: Any) {
if isLabelAtMaxHeight {
btn.setTitle("Read more", for: .normal)
isLabelAtMaxHeight = false
lblHeight.constant = 70
}
else {
btn.setTitle("Read less", for: .normal)
isLabelAtMaxHeight = true
lblHeight.constant = getLabelHeight(text: yourSummaryText, width: view.bounds.width, font: yourSummaryLabel.font)
}
}
Get height of a text
func getLabelHeight(text: String, width: CGFloat, font: UIFont) -> CGFloat {
let lbl = UILabel(frame: .zero)
lbl.frame.size.width = width
lbl.font = font
lbl.numberOfLines = 0
lbl.text = text
lbl.sizeToFit()
return lbl.frame.size.height
}
How to set label height for auto adjust in Read More/Less with Swift 3?
Remove constraint lblReviewHeight
, then just try to use numberOfLines
control your text layout, if your want show all description set numberOfLines = 0
, otherwise set numberOfLines
to the line you want.
Add ...Read More to the end of UILabel
So this is what I did to add the Read More... button to the UITextView
, UITextField
or UILabel
:
- (void)addReadMoreStringToUILabel:(UILabel*)label
{
NSString *readMoreText = @" ...Read More";
NSInteger lengthForString = label.text.length;
if (lengthForString >= 30)
{
NSInteger lengthForVisibleString = [self fitString:label.text intoLabel:label];
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:label.text];
NSString *trimmedString = [mutableString stringByReplacingCharactersInRange:NSMakeRange(lengthForVisibleString, (label.text.length - lengthForVisibleString)) withString:@""];
NSInteger readMoreLength = readMoreText.length;
NSString *trimmedForReadMore = [trimmedString stringByReplacingCharactersInRange:NSMakeRange((trimmedString.length - readMoreLength), readMoreLength) withString:@""];
NSMutableAttributedString *answerAttributed = [[NSMutableAttributedString alloc] initWithString:trimmedForReadMore attributes:@{
NSFontAttributeName : label.font
}];
NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:@{
NSFontAttributeName : Font(TWRegular, 12.),
NSForegroundColorAttributeName : White
}];
[answerAttributed appendAttributedString:readMoreAttributed];
label.attributedText = answerAttributed;
UITagTapGestureRecognizer *readMoreGesture = [[UITagTapGestureRecognizer alloc] initWithTarget:self action:@selector(readMoreDidClickedGesture:)];
readMoreGesture.tag = 1;
readMoreGesture.numberOfTapsRequired = 1;
[label addGestureRecognizer:readMoreGesture];
label.userInteractionEnabled = YES;
}
else {
NSLog(@"No need for 'Read More'...");
}
}
There is a use of fitString:intoLabel
method which can be found here.
As for the UITagTapGestureRecognizer
is just a normal UITapGestureRecognizer
subclass with a NSInteger
property called tag. I did that because I want to identify which Read More...
were clicked in I case I have more than one in the same UIViewController
. You can use a normal UITapGestureRecognizer
.
Enjoy!
Add Read More/Read Less to the end of UILabel SWIFT
Please try this:-
class FirstViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.text = "bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla"
myLabel.numberOfLines = 2
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.labelAction(gesture:)))
myLabel.addGestureRecognizer(tap)
myLabel.isUserInteractionEnabled = true
tap.delegate = self
}
@objc func labelAction(gesture: UITapGestureRecognizer)
{
if myLabel.numberOfLines == 0 {
myLabel.numberOfLines = 2
} else {
myLabel.numberOfLines = 0
}
}
}
If you want to handle using button then :
@IBAction func action(_ sender: Any) {
if myLabel.numberOfLines == 0 {
myLabel.numberOfLines = 2
} else {
myLabel.numberOfLines = 0
}
}
Also, you need to set constraints for the label height like below screenshot:
If you want to show more and less text at the end of label text then add more text and less text in the label accordingly.
Show more button next to end of text Swift
Here's a great CocoaPod for the functionality you're looking for.
https://github.com/ilyapuchka/ReadMoreTextView
This will allow you to set a maximum number of lines and customise the text append to the end of the trimmed text. You can change the font and color since it is an NSAttributedString
.
How To add ..Read More in UILabel
At first set the number of line = 1 or 2 which you want for show more button.
Like as
self.yourLabel?.numberOfLines = 2
self.yourLabel?.lineBreakMode = .ByWordWrapping
self.yourLabel?.sizeToFit()
then on show more button click set
self.yourLabel?.numberOfLines = 0
self.yourLabel?.sizeToFit()
if you used same button for both show less and show more than also maintain one boolean flag.
or For different ShowLess button click set the number of line to 2
Related Topics
What Does $0 Represent in Closures in Swift
Programmatically Detect Dark Mode in Swiftui to Display Appropriate Image
Cannot Invoke 'Indexof' with an Argument List of Type '(Checklistitem)'
Offline Crash Reporting in Crashlytics
How to Hide a Bar Button Item for Certain Users
iOS 13 Set Uisearchtextfield Placeholder Color
Chrome for iOS User Agent on iPad
Programmatically Sending an App to Background
Type 'Nsattributedstringkey' (Aka 'Nsstring') Has No Member 'Font'
Google 400 Error: Invalid Request Custom Scheme Uris Are Not Allowed for 'Web' Client Type
Avplayer with Playback Controls of Avplayerviewcontroller