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.
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
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
.
Related Topics
How to Handle Different Orientations in Ios
A Complete Solution to Locally Validate an In-App Receipts and Bundle Receipts on iOS 7
Create Thread Safe Array in Swift
When to Use Dequeuereusablecellwithidentifier VS Dequeuereusablecellwithidentifier: Forindexpath
Uibutton Inside a View That Has a Uitapgesturerecognizer
Change Uitableview Height Dynamically
How to Play Audio in Background With Swift
A Swift Example of Custom Views For Data Input (Custom In-App Keyboard)
Ios 7 Textkit - How to Insert Images Inline With Text
How to Use Facebook iOS Sdk on iOS 10
Ios App With Arc, Find Who Is Owner of an Object
How to Put Buttons Over Uitableview Which Won't Scroll With Table in Ios
@Property Retain, Assign, Copy, Nonatomic in Objective-C
Sorting Array Alphabetically With Number
In Swift, How to Declare a Variable of a Specific Type That Conforms to One or More Protocols
Xcode 6 Beta 2 Issue Exporting .Ipa: "Your Account Already Has a Valid iOS Distribution Certificate"