ios - TTTAttributedLabel 链接检测无法在 iOS8 中使用 swift

标签 ios uitableview swift ios8 tttattributedlabel

我想使用 TTTAttributedLabel 来检测 UITableViewCell 的标签中文本的链接,但它不起作用。我在 iOS8 上使用 swift。下面是 UITableViewCell 代码:

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
        }
    }
}

最佳答案

我认为您没有正确地配置您的自定义单元格

首先 在您的 customCell 声明并连接您的 IBOutlet-s。选择您的 textLabel 并将其类添加到 TTTAtributedLabel。您的自定义单元格应如下所示:

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 数据源和委托(delegate)的类中添加 TTTAtributedLabelDelegate

然后 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

然后,如果您有需要从 TTTAtributedLabelDelegate 执行的方法,请添加它们并进行计算。

希望对你有帮助

关于ios - TTTAttributedLabel 链接检测无法在 iOS8 中使用 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499658/

相关文章:

javascript - 如何为具有特定路径(React Native)的现有项目创建私有(private) Git 存储库?

ios - 小部件在表格 View swift ios的单元格中相互重叠

iphone - UITableViewCells 位于 UITableView 的中心

swift - 使用 CLGeocoder 调用时参数 #1 缺少参数

ios - 无法设置 ScrollView iOS Swift 的宽度

ios - 在 Swift 中截图并在顶部添加文本/水印

ios - 使用套接字推送 iOS Web 应用程序的通知?

ios - 如何显示来自不同类的警报

ios - 为什么 App 在 Xcode 中成为 iPhone 的背景时停止运行?

objective-c - 在 UITableView 滚动时为单元格设置动画