你如何使用NSAttributedString?

318
NSStringNSMutableStrings中不可能使用多种颜色。所以我听说过有一个NSAttributedString,它是在iPad SDK 3.2(或约为3.2)推出的,并在iPhone SDK 4.0 beta中可用。

我想要一个具有三种颜色的字符串。

我之所以不使用三个单独的 NSStrings,是因为这三个 NSAttributedString 字符串的长度经常发生变化,因此我宁愿不使用任何计算来重新定位三个单独的 NSString 对象。

如果可以使用 NSAttributedString,那么如何制作以下内容(如果不能使用 NSAttributed string,则如何实现):

alt text

编辑: 请记住,@"first"@"second"@"third" 将随时被其他字符串替换。因此,使用硬编码的 NSRange 值将无法起作用。


Swift中的属性字符串 - Suragch
NSAttributeString的Swift代码:https://dev59.com/o14c5IYBdhLWcg3wnrb9#27728516 - Kirit Modi
15个回答

2
我制作了一个库,使这个过程变得更加容易。请查看ZenCopy
您可以创建Style对象,并将其设置为键以供以后引用。就像这样:
ZenCopy.manager.config.setStyles {
    return [
        "token": Style(
            color: .blueColor(), // optional
            // fontName: "Helvetica", // optional
            fontSize: 14 // optional
        )
    ]
}

然后,您可以轻松构建字符串并对其进行样式设置,并具有参数 :)
label.attributedText = attributedString(
                                ["$0 ".style("token") "is dancing with ", "$1".style("token")], 
                          args: ["JP", "Brock"]
)

你可以通过正则表达式搜索轻松地进行样式设置!
let atUserRegex = "(@[A-Za-z0-9_]*)"
mutableAttributedString.regexFind(atUserRegex, addStyle: "token")

这将使用“token”样式来为所有以“@”开头的单词设置样式(例如@jpmcglone)。
我仍然需要让它与所有NSAttributedString提供的功能一起正常工作,但我认为fontNamefontSize和color已经覆盖了大部分内容。很快会有很多更新:)
如果您需要帮助入门,我可以帮助您。同时也在寻求反馈,如果这使您的生活更轻松,那么我会说任务完成了。

1
- (void)changeColorWithString:(UILabel *)uilabel stringToReplace:(NSString *) stringToReplace uiColor:(UIColor *) uiColor{
    NSMutableAttributedString *text =
    [[NSMutableAttributedString alloc]
     initWithAttributedString: uilabel.attributedText];

    [text addAttribute: NSForegroundColorAttributeName value:uiColor range:[uilabel.text rangeOfString:stringToReplace]];

    [uilabel setAttributedText: text];

}

0

Swift 4

let combination = NSMutableAttributedString()

var part1 = NSMutableAttributedString()
var part2 = NSMutableAttributedString()
var part3 = NSMutableAttributedString()

let attrRegular = [NSAttributedStringKey.font : UIFont(name: "Palatino-Roman", size: 15)]

let attrBold:Dictionary = [NSAttributedStringKey.font : UIFont(name: "Raleway-SemiBold", size: 15)]

let attrBoldWithColor: Dictionary = [NSAttributedStringKey.font : UIFont(name: "Raleway-SemiBold", size: 15),
                                 NSAttributedStringKey.foregroundColor: UIColor.red]

if let regular = attrRegular as? [NSAttributedStringKey : NSObject]{
    part1 = NSMutableAttributedString(string: "first", attributes: regular)

}
if let bold = attrRegular as? [NSAttributedStringKey : NSObject]{
    part2 = NSMutableAttributedString(string: "second", attributes: bold)
}

if let boldWithColor = attrBoldWithColor as? [NSAttributedStringKey : NSObject]{
    part3 = NSMutableAttributedString(string: "third", attributes: boldWithColor)
}

combination.append(part1)
combination.append(part2)
combination.append(part3)

属性列表请参见此处 Apple文档中的NSAttributedStringKey


0

这是一种非常简单的方法。

let text = "This is a colorful attributed string"
let attributedText = 
NSMutableAttributedString.getAttributedString(fromString: text)
attributedText.apply(color: .red, subString: "This")
//Apply yellow color on range
attributedText.apply(color: .yellow, onRange: NSMakeRange(5, 4))

更多详情请点击这里:https://github.com/iOSTechHub/AttributedString


0
为了解决这类问题,我在Swift中创建了一个名为Atributika的库。
let str = "<r>first</r><g>second</g><b>third</b>".style(tags:
        Style("r").foregroundColor(.red),
        Style("g").foregroundColor(.green),
        Style("b").foregroundColor(.blue)).attributedString

label.attributedText = str

你可以在这里找到它 https://github.com/psharanda/Atributika

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接