使用NSAttributedString的UILabel的NSForegroundColorAttributeName [英] NSForegroundColorAttributeName for UILabel using NSAttributedString

查看:126
本文介绍了使用NSAttributedString的UILabel的NSForegroundColorAttributeName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含链接的NSAttributed字符串,我想将其加载到UILabel中.我工作正常,但是所有链接均为蓝色.

I have NSAttributed string with links in it and I want to load it inside UILabel. I works fine, however all links are blue Color.

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

不更改前景色,设置所有其他属性都起作用,例如删除线样式.只是链接始终保持蓝色.

No change to foreground color, setting all other attributes work, like strikethrough style. Just link always stays blue.

如果有任何区别,可以从HTML生成NSAttributed字符串.

NSAttributed string is generated from HTML if that makes any difference.

推荐答案

最终做完

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拥有自己的链接格式,所以结束了

Does the job, took a while to figure out. Because UILabel has its own link formatting ended up

  • 设置字符串后,从属性字符串中删除所有链接
  • 将链接和范围添加到数组
  • 选择链接后,使用NSTextContainer找出字符所在的索引
  • 查找人物所属的范围
  • 返回链接

这篇关于使用NSAttributedString的UILabel的NSForegroundColorAttributeName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆