Giulio Colleluori
Giulio Colleluori

Reputation: 1361

Multiple lines UILabel with multiple buttons and fonts

I'm making a UITableViewCell for some activity feed objects, to give you an idea they're going to be like the Facebook posts where you have multiple links in one post.

In my case there are going to be two links to other UIViewController for each post and one plain UILabel that connects the two and explains the connection (such as "X has commented on Y's post") where you could tap both X and Y for some actions to happen.

As right now, I just made 3 separate UILabels. The problem with that is that I'm not sure how to handle names that are too long in multiple lines.

Meaning if for example instead of X, the post was "XXXXXXXXXXXXXXXXX has commented on Y's post", then "has commented on Y's post" would need to go on another line.

As right now I just link the 3 UILabels with constraints such that they're next to each other but that wouldn't work when they're too long.

If you have any idea on how could I approach this issue, it would be really appreciated if you could let me know.

Thanks in advance.

Upvotes: 0

Views: 197

Answers (2)

Ketan Parmar
Ketan Parmar

Reputation: 27428

Set number of line of label to zero like,

 label.numberOfLines = 0     // it will increase line when needed

And use attribute string to for different fonts and size or links etc.

and then set that string to label as,

    label.attributedText = attributedString

Update :

If you want some action event on particular text then you should use UITextview instead of label. it will be more easier by implementing shouldInteractWithURL delegate of it.

Another good solution is use thirdparty library like TTTAttributedLabel. It is exact what you want i think. have a look once.

Update 2 :

refer this link or this link for your desired output

Hope this will help :)

Upvotes: 0

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

There are too many labels, I think you can use this extension:

extension NSMutableAttributedString {

   public func setAsLink(textToFind:String, linkURL:String) -> Bool {

       let foundRange = self.mutableString.rangeOfString(textToFind)
       if foundRange.location != NSNotFound {
           self.addAttribute(NSLinkAttributeName, value: linkURL, range: foundRange)
           return true
       }
       return false
   }
}

Then you can do:

let labelFont = UIFont(name: "HelveticaNeue-Bold", size: 18)
let attributes :[String:AnyObject] = [NSFontAttributeName : labelFont!]
let attrString = NSAttributedString(string:"foo www.google.com", attributes: attributes)
let urlPath: String = "http://www.google.com"
let url: NSURL = NSURL(string: urlPath)!
attrString.setAsLink("www.google.com", linkURL:url) 
myLabel.attributedText = attrString

UPDATE: (after your comments)

If you need to intercept urllink you can transform your label to textView (UITextView), set it the delegate and handle the shouldInteractWithURL method:

class ViewController: UIViewController, UITextViewDelegate { 
... // on your code do:
    myTextView.delegate = self
...
func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {

   if URL.scheme == "http://www.google.com" {
      //do whatever you want
      launchMyMethodForThisUrl()
   }

}

Upvotes: 1

Related Questions