NSForegroundColorAttributeName for UILabel using NSAttribute



我有带有链接的NSAttributed字符串,我想将其加载到UILabel中。我工作正常,但是所有链接都是蓝色的。

let string = NSMutableAttributedString(attributedString: attributedText)
string.addAttributes([NSForegroundColorAttributeName:linkColor], range: linkRange)
self.attributedText = string

前景色不变,设置所有其他属性都有效,例如删除线样式。只是链接始终保持蓝色。

NSA归因字符串是从HTML生成的,如果这有任何区别的话。

最终做了

class AttributedTextLabel:UILabel {
var attributedString:NSAttributedString?{
    didSet{
        guard let attributedString = attributedString else {
            return
        }
        let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
        mutableAttributedString.enumerateAttribute(NSLinkAttributeName, inRange: NSRange(location: 0, length: attributedString.length), options: NSAttributedStringEnumerationOptions.Reverse) {[weak self] (attribute, range, other) in
            if let url = attribute as? NSURL {
                mutableAttributedString.removeAttribute(NSLinkAttributeName, range: range)
                self?.links.append(Link(url: url, range: range))
            }
        }
        self.attributedText = mutableAttributedString
    }
}
struct Link {
    var url:NSURL
    var range:NSRange
}
var links:[Link] = []
var edgeInsets:UIEdgeInsets = UIEdgeInsetsZero
private var textContentSize:CGSize {
    let textContainerWidth = frame.width - edgeInsets.left - edgeInsets.right
    let textContainerHeight = frame.height - edgeInsets.top - edgeInsets.bottom
    return CGSizeMake(textContainerWidth, textContainerHeight)
}

func characterIndexAtPoint(point:CGPoint) -> Int? {
    guard let attributedText = attributedText else {
        return nil
    }
    let layoutManager = NSLayoutManager()
    let textContainer = NSTextContainer(size: textContentSize)
    textContainer.lineFragmentPadding = 0.0
    textContainer.lineBreakMode = self.lineBreakMode
    textContainer.maximumNumberOfLines = self.numberOfLines
    layoutManager.addTextContainer(textContainer)
    let storage = NSTextStorage(attributedString: attributedText)
    storage.addLayoutManager(layoutManager)
    let adjustedPoint = CGPointMake(point.x-edgeInsets.left, point.y-edgeInsets.top)
    let characterIndex = layoutManager.characterIndexForPoint(point, inTextContainer: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
    return characterIndex
}
override func drawTextInRect(rect: CGRect) {
    return super.drawTextInRect(UIEdgeInsetsInsetRect(rect, edgeInsets))
}
private var selectedRange:NSRange?
private var highligtedLink:Link? {
    didSet{
        let string = self.attributedText as! NSMutableAttributedString
        if let oldValue = oldValue {
            if let selectedLinkColor = NativeTextKit.TextAttributes.selectedLinkColor.value {
                string.addAttributes([
                    NSForegroundColorAttributeName:selectedLinkColor
                    ], range: oldValue.range)
            }
        }
        if let highligtedLink = highligtedLink {
            if let selectedLinkColor = NativeTextKit.TextAttributes.selectedLinkColor.value {
                string.addAttributes([
                    NSForegroundColorAttributeName:selectedLinkColor
                    ], range: highligtedLink.range)
            }
        }
        self.attributedText = string
    }
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    let char = characterIndexAtPoint(touch.locationInView(self))
    let string = self.attributedText as! NSMutableAttributedString
    highligtedLink = linkForTouch(touch)
    string.addAttributes([
        NSForegroundColorAttributeName:UIColor.brownColor()
        ], range: NSMakeRange(char!, 1))
    attributedText = string
}
func linkForTouch(touch:UITouch)->Link? {
    guard let attributedText = attributedText else {
        return nil
    }
    guard let characterIndex = characterIndexAtPoint(touch.locationInView(self)) else {
        return nil
    }
    return links.filter({NSLocationInRange(characterIndex, $0.range)}).first
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    highligtedLink = nil
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    if let highligtedLink = highligtedLink, let lastTouchedLink = linkForTouch(touch) where highligtedLink.url == lastTouchedLink.url {
        urlInteractionHandler?(textView: UITextView(), url:lastTouchedLink.url)
    }
}
/// Executed on link interaction
var urlInteractionHandler:URLInteractionHandler?
}

做这项工作,花了一段时间才弄清楚。因为UILabel有自己的链接格式最终

  • 设置字符串后,从属性字符串中删除所有链接
  • 向阵列添加链接和范围
  • 选择链接后,使用 NSTextContainer 找出字符的索引
  • 查找字符所属的范围
  • 返回链接

最新更新