2017-09-14 88 views
1

我正在開發一個社交應用程序,我需要顯示帖子和共享等。 我還需要顯示標籤的朋友。如何創建可點擊標籤與ios中的Facebook一樣?

當帖子在列表中顯示時UI也與Facebook帖子類似。請檢查下面的屏幕截圖。 enter image description here

正如你可以看到點擊文章標題是由於單行線,哪裏需要打開用戶配置文件的時候,他的名字用戶點擊即人名爲abc,想打開的頁面用戶點擊來自Buzzfeed印度時

由於我們需要使用UILabel來顯示像這樣的內容,我無法獲得特定單詞的點擊。我已經試過https://github.com/null09264/FRHyperLabel但是當有多行時單擊單詞並不完美。

+0

您可在此找到答案:** HTTPS://stackoverflow.com/questions/20541676/ios-uitextview-or-uilabel- with-clickable-links-to-actions ** –

回答

1

您可以使用TTTAttributedLabel來製作UILabel中的特定部分或單詞。

例如

label.text = @"Fork me on GitHub!"; // Repository URL will be automatically detected and linked 
NSRange range = [label.text rangeOfString:@"me"]; 
[label addLinkToURL:[NSURL URLWithString:@"meUrl"] withRange:range]; 

這裏我變成可點擊,並在攻這個詞的委託方法didSelectLinkWithURL被調用,您可以檢查該鏈接功能

(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { 
    NSLog(@"link %@", [url absoluteString]); 
    if ([url absoluteString] == @"meUrl") { 
    //implement your logic here 
    } 
    NSLog(@"whole label %@", label); 
} 
+0

你的答案適合我。但唯一的問題是在可點擊的單詞下方顯示下劃線。我如何刪除該行。 https://prnt.sc/gl1csa –

+0

你可以通過賦予屬性文本來標記具有所需的顏色屬性 –

+0

我可以按照這個答案刪除可點擊的字下劃線https://stackoverflow.com/questions/15381028/make -u-uilabel-attributestext-not-blue-and-not-underlined –

0

嘗試使用這裏面:

添加擴展程序

extension UITapGestureRecognizer { 

    func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool { 
     // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage 
     let layoutManager = NSLayoutManager() 
     let textContainer = NSTextContainer(size: CGSize.zero) 
     let textStorage = NSTextStorage(attributedString: label.attributedText!) 

     // Configure layoutManager and textStorage 
     layoutManager.addTextContainer(textContainer) 
     textStorage.addLayoutManager(layoutManager) 

     // Configure textContainer 
     textContainer.lineFragmentPadding = 0.0 
     textContainer.lineBreakMode = label.lineBreakMode 
     textContainer.maximumNumberOfLines = label.numberOfLines 
     let labelSize = label.bounds.size 
     textContainer.size = labelSize 

     // Find the tapped character location and compare it to the specified range 
     let locationOfTouchInLabel = self.location(in: label) 
     let textBoundingBox = layoutManager.usedRect(for: textContainer) 
     let textContainerOffset = CGPoint(x:(labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,y:(labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y); 


     let locationOfTouchInTextContainer = CGPoint(x:locationOfTouchInLabel.x - textContainerOffset.x,y:locationOfTouchInLabel.y - textContainerOffset.y); 
     let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil) 

     return NSLocationInRange(indexOfCharacter, targetRange) 
    } 

} 

然後在您的UILable上添加UITapGestureRecognizer。請確保UILabel.isUserInteractionEnabled是真的

override func viewDidLoad() { 
     super.viewDidLoad() 

     initialSetup() 
    } 

func initialSetup(){ 
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.didTap(_:))) 
    lblTitle.addGestureRecognizer(tap) 
    lblTitle.isUserInteractionEnabled = true 
    lblTitle.numberOfLines = 0 
    let personname = "Person Name Abc" 
    let buzzIndia = "Buzzfeed india" 
    let str = "\(personname) shared \(buzzIndia)'s video" 
    let attributedString = NSMutableAttributedString(string:str) 

    let range = (str as NSString).range(of: personname) 
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: range) 
    if let font = UIFont(name: "Helvetica Bold", size: 14) { 
     attributedString.addAttribute(NSFontAttributeName, value: font, range: range) 
    } 

    let rangeBuzz = (str as NSString).range(of: buzzIndia) 
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: rangeBuzz) 
    if let font = UIFont(name: "Helvetica Bold", size: 14) { 
     attributedString.addAttribute(NSFontAttributeName, value: font, range: rangeBuzz) 
    } 

    lblTitle.attributedText = attributedString 

} 

func didTap(_ gesture: UITapGestureRecognizer) { 
    let text = (lblTitle.text)! 
    let name = (text as NSString).range(of: "Person Name Abc") 
    let buzzfeed = (text as NSString).range(of: "Buzzfeed india") 

    if gesture.didTapAttributedTextInLabel(label: lblTitle, inRange: name) { 
     //Repective "Person Name Abc" action 
     print("Person Name Abc") 
    }else if gesture.didTapAttributedTextInLabel(label: lblTitle, inRange: buzzfeed) { 
     //Repective "Buzzfeed india" action 
     print("Buzzfeed india") 
    }else { 
     print("other tapped") 
    } 
} 

參考:http://samwize.com/2016/03/04/how-to-create-multiple-tappable-links-in-a-uilabel/