I have to used a show more text after a certain limit of uilabel. When i click on that uilabel show more text the label should expand and the show more text should change to show less .
Asked
Active
Viewed 1,176 times
2
-
you need to use tableview. – Brijesh Shiroya Jun 29 '17 at 06:16
-
@BrijeshShiroya is there any option other than Tableview? – Shibanee Mishra Jun 29 '17 at 06:19
-
You need to use `NSAttributed` Text in your lable @ShibaneeMishra – Dhiru Jun 29 '17 at 06:19
-
1Possible duplicate of [Add "...Read More" to the end of UILabel](https://stackoverflow.com/questions/32309247/add-read-more-to-the-end-of-uilabel) – Lal Krishna Jun 29 '17 at 06:21
2 Answers
5
Use the following code for for answer.
- (void)addReadMoreStringToUILabel:(UILabel*)label
{
NSString *readMoreText = @" ...Read More";
NSInteger lengthForString = label.text.length;
if (lengthForString >= 100)
{
NSInteger lengthForVisibleString = 100;
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
}];
UIColor *color = [UIColor colorWithRed:21.0/255.0 green:40.0/255.0 blue:86.0/255.0 alpha:1]; // select needed color
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color, NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};
NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:attrs];
[answerAttributed appendAttributedString:readMoreAttributed];
label.attributedText = answerAttributed;
UITapGestureRecognizer *readMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(readMoreDidClickedGesture:)];
readMoreGesture.numberOfTapsRequired = 1;
[label addGestureRecognizer:readMoreGesture];
label.userInteractionEnabled = YES;
}
else
{
NSLog(@"No need for 'Read More'...");
}
}
-(void)readMoreDidClickedGesture:(id)sender
{
if(ReadMoretag==1)
{
ReadMoretag=0;
NSString *readMoreText = @" Read Less";
UIColor *color = [UIColor colorWithRed:21.0/255.0 green:40.0/255.0 blue:86.0/255.0 alpha:1]; // select needed color
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color };
NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:attrs];
NSMutableAttributedString *answerAttributed = [[NSMutableAttributedString alloc] initWithString:StrProfileSummary attributes:@{
NSFontAttributeName : LblProfilEsummary.font
}];
[answerAttributed appendAttributedString:readMoreAttributed];
LblProfilEsummary.attributedText = answerAttributed;
}
else
{
ReadMoretag=1;
[self addReadMoreStringToUILabel:LblProfilEsummary];
}
}

Raman Srivastava
- 396
- 1
- 14
-
-
its ok but exactly not working in new larger size device i.e. iPhone Xs Max, iPhones XS, iPhones XR, – Ravi Raja Jangid Sep 26 '18 at 07:49
1
CGFloat width = ([UIScreen mainScreen].bounds.size.width - 40);
NSMutableAttributedString *stringText;
NSInteger oneLineHeight = [CommonMethods findHeightForText:@"A" havingWidth:width andFont:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0)].height;
NSInteger totalHeight = [CommonMethods findHeightForText:_strCaption havingWidth:width andFont:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0)].height;
NSInteger noOfLines = totalHeight/oneLineHeight;
if(noOfLines > kNumberOfLines) {
CGSize size = CGSizeMake(width, 42);
NSInteger maxCharLength = [self findPageSplits:_strCaption size:size font:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0)];
NSLog(@"%ld",(long)maxCharLength);
NSString *strText = [_strCaption substringWithRange:NSMakeRange(0, maxCharLength-10)];
strText = [NSString stringWithFormat:@"%@... More",strText];
NSLog(@"%@",strText);
stringText = [[NSMutableAttributedString alloc] initWithString:strText];
[stringText addAttribute: NSFontAttributeName value:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0) range:[strText rangeOfString:ellipsis]];
[stringText addAttribute: NSForegroundColorAttributeName value:RGB120200230 range:[strText rangeOfString:ellipsis]];
}
else
{
stringText = [[NSMutableAttributedString alloc] initWithString:_strCaption];
}
Use this method for page spliting
- (NSInteger) findPageSplits:(NSString*)string size:(CGSize)size font:(UIFont*)font;
{
//Session Content Split
CTFontRef fnt;
CFAttributedStringRef str;
CTFramesetterRef fs;
fnt = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize,NULL);
str = CFAttributedStringCreate(kCFAllocatorDefault,
(CFStringRef)string,
(CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fnt,kCTFontAttributeName,nil]);
fs = CTFramesetterCreateWithAttributedString(str);
CFRange r = {0,0};
CFRange res = {0,0};
CTFramesetterSuggestFrameSizeWithConstraints(fs,r, NULL, size, &res);
return res.length;
}

Vidhi Patel
- 603
- 5
- 10