2016-11-30 3 views
1

htmlテキストを表示するためにASTextNode(AsyncDisplayKitのUILabelと同じ)を表示しようとしています。ラベルの属性付きテキストを設定するだけです。NSAttributedString htmlリンクの色を変更します。

私は私の文字列をどのように働くかある:

この拡張機能を使用して、私はNSAttributedStringにHTMLテキストを変換:

extension String { 
    var html2AttributedString: NSAttributedString? { 
     guard let data = data(using: .utf8) else { return nil } 
     do { 
      return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) 
     } catch let error as NSError { 
      print(error.localizedDescription) 
      return nil 
     } 
    } 
    var html2String: String { 
     return html2AttributedString?.string ?? "" 
    } 
} 

それから私は私のラベルの詳細を設定します。

self.displayContent = NSMutableAttributedString(attributedString: content.html2AttributedString!) 
self.displayContent?.addAttribute(NSFontAttributeName, value: UIFont.fontMainFeedContentFont(), range: NSRange.init(location: 0, length: self.displayContent!.length)) 

ので、私は自分のフォントで自分のラベルを持っています。問題は私のラベルのリンクの色を変えることができないということです。私はそれがシステムの青です。

どのようにリンクの色を変更できますか?

ありがとうございました。

+0

、あなたを行います下線の色を意味しますか? – KrishnaCA

+0

いいえ、テキストの色は黒、リンクの色は青です – user2206906

答えて

0

リンクの色は、次のように変更することができます。以下の例は、そのデモンストレーションを行います:

let attributedText:NSMutableAttributedString = NSMutableAttributedString(string: "why?") 
attributedText.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle, range: NSMakeRange(0, attributedText.length)) 
attributedText.addAttribute(NSUnderlineColorAttributeName, value: UIColor.black, range: NSMakeRange(0, attributedText.length)) 
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSMakeRange(0, attributedText.length)) 

はまた、あなたがそれを表示していますUITextViewに次の変更を行う必要があります。

textView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.black] 

あなたがこれを行うにはUITextViewを使用したくない場合は、単にTTTAttributedLabelを使用することができます。 UITextViewを使用せずに目的の動作を達成するために使用できるlinkAttributesactiveLinkAttributesプロパティがあります。

動作するかどうか教えてください。お気軽にこれを改善するための編集を提案してください:)

+0

これは、私が使用するテキストビューではありません、私はtableviewではない、私は高さを計算したくない、私はあなたに感謝しようとします – user2206906

+0

NSUnderlineStyleAttributeNameは単に下線リンク、私はリンクのテキストの色を変更したい。 – user2206906

+0

'attributedText.addAttribute(NSForegroundColorAttributeName、value:UIColor.black、range:NSMakeRange(0、attributedText.length))'を使って、すでに試してみたと思います。それは私のために働かなかった。 'UITextView'だけが' linkTextAttributes'プロパティを持っていて、coretextを使わない唯一の方法です。 – KrishnaCA

0

ええ、みんな、これを行うには醜い方法を見つけました。

がNSMutableAttributedStringにHTMLテキストを変換した後、私は単にループiが「NSLink」属性を見ると、私は単に属性の範囲の属性を追加し、すべての属性を考えた:リンクの色によって

self.myString!.enumerateAttributes(in: NSRange(0..<myString!.length), options: []) { (attributes, range, _) -> Void in 
       for (attribute, object) in attributes { 
        if attribute == "NSLink" { 
         print("Attribute = \(attribute) -- \(object)") 
         self.myString?.addAttribute(NSForegroundColorAttributeName, value: StyleKit.color_blue_bright, range: range) 
         self.myString?.addAttribute(NSUnderlineColorAttributeName, value: UIColor.clear, range: range) 
        } 
       } 
      } 
関連する問題