Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append readmore label after 120 char, and make it clickable in ios

Tags:

ios

swift

The data is coming from parser, if the text is more than 120 char then it should append "...ReadMore" just like facebook. I have got the code for appending text, but dont know how to make clickable link. I'm using Swift Languange.

if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120
    {
     var abc : String =  (cell!.bhikmangaTextlbl!.text! as 
    NSString).substringWithRange(NSRange(location: 0, length: 120))
     abc += " ...ReadMore"
      cell!.bhikmangaTextlbl!.text = abc

    }
like image 894
Zubin Gala Avatar asked Aug 24 '15 05:08

Zubin Gala


2 Answers

@Mihir Mehta, This is code i have implemented.

    var bhikmangaTextlbl:UITextView?  
if cell!.bhikmangaTextlbl!.text!.utf16Count >= 120
            {
                var abc : String =  (cell!.bhikmangaTextlbl!.text! as NSString).substringWithRange(NSRange(location: 0, length: 120))
                    abc += "...ReadMore"
                    cell!.bhikmangaTextlbl!.text = abc
                var attribs = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(14.0)]
                var attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs)
                attributedString.addAttribute(NSLinkAttributeName, value: "...ReadMore", range: NSRange(location: 120, length: 11))
                attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSRange(location: 120, length: 11))
                cell!.bhikmangaTextlbl!.attributedText = attributedString
            }


     //The function you said i have written here
        func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
            {
                return true
            }
like image 106
Zubin Gala Avatar answered Sep 28 '22 00:09

Zubin Gala


You can use attributed string here ... and make some part of text clickable

here is Objective C code. You can write same for Swift

NSDictionary *attribs = @{
                              NSForegroundColorAttributeName: BODY_FONT_COLOR,
                              NSFontAttributeName: [UIFont fontWithName:FontHelvetica size:AppFont16]
                              };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:ACCIDENT_DETECTION_AUTOMATIC_TEXT attributes:attribs];
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"Accident detection"
                             range:[[attributedString string] rangeOfString:@"Read More"]];




    NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: LINKS_FONT_COLOR,
                                     NSUnderlineColorAttributeName: [UIColor clearColor],
                                     NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};

    // assume that textView is a UITextView previously created (either by code or Interface Builder)
    self.accidentDetectionText.linkTextAttributes = linkAttributes; // customizes the appearance of links
    self.accidentDetectionText.attributedText = attributedString;
like image 25
Mihir Mehta Avatar answered Sep 27 '22 23:09

Mihir Mehta