我想使用 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/