2014-10-22 30 views
8

我想使用TTTAttributedLabel檢測UITableViewCell標籤中的文本鏈接,但它不起作用。我在iOS8上使用swift。下面是UITableViewCell的代碼:TTTAttributedLabel鏈接檢測在iOS8中無法使用swift

class StoryTableViewCell: UITableViewCell, TTTAttributedLabelDelegate { 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 

     // Link properties 
     let textLabel = self.descriptionLabel 
     let linkColor = UIColor(red: 0.203, green: 0.329, blue: 0.835, alpha: 1) 
     let linkActiveColor = UIColor.blackColor() 

     if (textLabel is TTTAttributedLabel) { 
      var label = textLabel as TTTAttributedLabel 
      label.linkAttributes = [NSForegroundColorAttributeName : linkColor] 
      label.activeLinkAttributes = [NSForegroundColorAttributeName : linkActiveColor]   
      label.enabledTextCheckingTypes = NSTextCheckingType.Link.toRaw() 

      label.delegate = self 
     } 
    } 
} 
+0

查看:http://stackoverflow.com/a/22395136/1106035 – 2014-10-22 04:28:27

+0

@Pince它不工作 – JeremyWei 2014-10-22 07:43:19

回答

12

我想你還沒有配置您的custom cell正確

第一個在您的customCell聲明並連接您的IBOutlet-s。選擇您的textLabel並將其類添加到TTTAttributedLabel。您的自定義單元格應該是這樣的:

class StoryTableViewCell: UITableViewCell { 
    @IBOutlet weak var textLabel: TTTAttributedLabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
} 

你需要一個正在使用的數據源的tableView和委託的類以添加TTTAttributedLabelDelegate

然後cellForRowAtIndexPath

var cell: StoryTableViewCell = tableView.dequeueReusableCellWithIdentifier("yourCellIdentifier") as StoryTableViewCell 

let linkColor = UIColor(red: 0.203, green: 0.329, blue: 0.835, alpha: 1) 
let linkActiveColor = UIColor.blackColor() 

cell.textLabel.delegate = self 

cell.textLabel.linkAttributes = [kCTForegroundColorAttributeName : linkColor] 
cell.textLabel.activeLinkAttributes = [kCTForegroundColorAttributeName : linkActiveColor]   
cell.textLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue 

然後,如果你有需要從TTTAttributedLabelDelegate執行添加它們,做你的計算方法。

希望它可以幫助

+0

我跟着你的答案,但我無法使它的工作。有關如何調試它的任何想法?文本在那裏,但沒有找到鏈接。 – rsc 2015-02-14 08:00:16

+0

更新1:我開始調試,通過添加委託函數belongsLabel didSelectLinkWithURL和didLongPressLinkWithURL。前者不起作用,但後者是。 更新2:didSelectLinkWithURL沒有被調用,因爲我在tapper.cancelsTouchesInView = true的視圖中有一個tapper。當我將它設置爲false時,點擊開始工作。 – rsc 2015-02-14 08:09:26

+5

看起來NSForegroundColorAttributeName在iOS8上不起作用。如果發生這種情況,請改用kCTForegroundColorAttributeName。 – rsc 2015-02-14 09:14:22

0
let linkColor = UIColor.blueColor() 
    let linkActiveColor = UIColor.greenColor() 

    textLabel.delegate = self 

    textLabel.linkAttributes = [kCTForegroundColorAttributeName : linkColor.CGColor,kCTUnderlineStyleAttributeName : NSNumber(bool: true)] 
    textLabel.activeLinkAttributes = [NSForegroundColorAttributeName : linkActiveColor] 
    textLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue 
+0

嗨kamlesh我正在面對tttattributedlabel中的截斷標記點擊問題。你有這個想法嗎? – Aditya 2016-06-09 07:08:26

3

如果您已設置TTTAttributedLabel作爲類你的UILabel的,筆尖或故事板內,確保用戶交互啓用設置爲true,如被默認情況下, UILabel將禁用用戶交互。