xcode 为 NSAttributedstring 的不同部分应用不同的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8520544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:45:10  来源:igfitidea点击:

Applying different attributes for different portions of an NSAttributedstring

iosxcodensattributedstring

提问by Arun

I know it is possible to apply attributes to an NSAttributedString.

我知道可以将属性应用于 NSAttributedString。

But how can we apply different attributes to same attributed string.

但是我们如何将不同的属性应用于相同的属性字符串。

for the string "This is an attributed text."

对于字符串“这是一个属性文本”。

How can we set a particular attribute(background color or foreground color) to "This is" another attribute(background color or foreground color) to "an attributed text."

我们如何将特定属性(背景色或前景色)设置为“这是”另一个属性(背景色或前景色)为“属性文本”。

Is there any way to achieve this....?

有什么办法可以实现这一目标......?

Also is there any way to set the background color of an NSAttributedString in iOS?

还有什么方法可以在 iOS 中设置 NSAttributedString 的背景颜色?

回答by Isabel

Make a mutable copy of the attributed string, and then use either:

制作属性字符串的可变副本,然后使用:

-setAttributes:range:
-addAttributes:range:
-addAttribute:value:range:
-removeAttributes:range:

-setAttributes:range:
-addAttributes:range:
-addAttribute:value:range:
-removeAttributes:range:

for example, to set a red color for the first four letters:

例如,为前四个字母设置红色:

NSMutableAttributedString *mutAttrStr = [attributedString mutableCopy];
CGColorRef redClr = [UIColor redColor].CGColor;
NSDictionary *newAttributes = [NSDictionary dictionaryWithObject:(id)redClr forKey:(id)kCTForegroundColorAttributeName];
[mutAttrStr addAttributes:newAttributes range:NSMakeRange(0, 4)];

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html

There's no built-in way to draw the background color on iOS. You can create a custom string constant for the attribute, eg "MyBackgroundColorAttributeName", and then you'll have to draw it yourself.

在 iOS 上没有绘制背景颜色的内置方法。您可以为属性创建自定义字符串常量,例如“MyBackgroundColorAttributeName”,然后您必须自己绘制它。

回答by Atka

I am using the following extension to change "NSMutableAttributedString" colour and it did the trick nicely. It can be used as a template

我正在使用以下扩展来更改“NSMutableAttributedString”颜色,它很好地完成了这个技巧。它可以用作模板

extension NSMutableAttributedString {

    func changeColourOf(_ terms: [String], toThisColour termsColour: UIColor, 
                        andForegroundColour fontColour: UIColor) {

        // Set your foreground Here
        addAttributes([NSAttributedString.Key.foregroundColor : fontColour], 
                      range: NSMakeRange(0, self.length))

        // Convert "NSMutableAttributedString" to a NSString  
        let string = self.string as NSString

        // Create a for loop for ther terms array
        for term in terms {
            // This will be the range of each term
            let underlineRange = string.range(of: term)

            // Finally change the term colour
            addAttribute(NSAttributedString.Key.foregroundColor, 
                         value: termsColour, 
                         range: underlineRange)
        }
    }
}

Example:

例子:

let someMutableStr = NSMutableAttributedString(string: "Hello World 2017 !")
someMutableStr.changeColourOf(["Hello", "2017"], 
                              toThisColour: .blue, 
                              theStringFontColour: .red)

enter image description here

在此处输入图片说明