NSMutableAttributedString

NSMutableAttributedString

本文介绍了更改NSMutableAttributedString中链接的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但我的链接始终是蓝色的。如何设置它们的颜色?

I have the following code but my links are always blue. How do I cange the color of them?

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)];
[_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)];
[_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)];

_string是一个NSMutableAttributedString,位置和长度工作正常。

_string is a NSMutableAttributedString and the position and length work fine.

推荐答案

Swift



更新为Swift 3

使用 linkTextAttributes UITextView

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green]

在上下文中:

let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
let linkRange = (attributedString.string as NSString).range(of: "@marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange)
let linkAttributes: [String : Any] = [
    NSForegroundColorAttributeName: UIColor.green,
    NSUnderlineColorAttributeName: UIColor.lightGray,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]

// textView is a UITextView
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString
textView.delegate = self

Swift 4:

let linkAttributes: [String : Any] = [
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.green,
    NSAttributedStringKey.underlineColor.rawValue: UIColor.lightGray,
    NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue
]

Swift 4.2 :

let linkAttributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.foregroundColor: UIColor.green,
    NSAttributedString.Key.underlineColor: UIColor.lightGray,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single
]



Objective-C



使用 linkTextAttributes 使用 UITextView

textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};

资料来源:

来自:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"username://marcelofabri_"
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;

这篇关于更改NSMutableAttributedString中链接的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 08:17