如何使用不同的字体追加两个属性字符串?

时间:2015-08-21 13:39:27

标签: ios swift fonts uikit nsattributedstring

在这里,我试图在一个UILabel中追加两个属性字符串,但我犯了一些错误,所以任何人都可以找到我的错误吗?

let textFontAttributes = [
            NSFontAttributeName : font,
            // Note: SKColor.whiteColor().CGColor breaks this
            NSForegroundColorAttributeName: UIColor.whiteColor(),
            NSStrokeColorAttributeName: UIColor.blackColor(),
            // Note: Use negative value here if you want foreground color to show
            NSStrokeWidthAttributeName: -8]

1 个答案:

答案 0 :(得分:4)

这是演示,你可以尝试一下,事实上,你应该提供更多关于错误是什么或错误是什么的信息。

var firstString  = "Hello" as NSString

var secondString = " World"  as NSString

var totalString = firstString.stringByAppendingString(secondString as String) as NSString

let firsttAttributes = [
    NSFontAttributeName : UIFont.systemFontOfSize(16),
    // Note: SKColor.whiteColor().CGColor breaks this
    NSForegroundColorAttributeName: UIColor.whiteColor(),
    NSStrokeColorAttributeName: UIColor.blackColor(),
    // Note: Use negative value here if you want foreground color to show
    NSStrokeWidthAttributeName: -8]

let secondAttributes = [
    NSFontAttributeName : UIFont.systemFontOfSize(12),
    // Note: SKColor.whiteColor().CGColor breaks this
    NSForegroundColorAttributeName: UIColor.redColor(),
    NSStrokeColorAttributeName: UIColor.blackColor(),
    // Note: Use negative value here if you want foreground color to show
    NSStrokeWidthAttributeName: -8]

var attributedString = NSMutableAttributedString(string: totalString as String, attributes: firsttAttributes)

var secondRange = totalString.rangeOfString(secondString as String)

if secondRange.location != NSNotFound {
    attributedString.addAttributes(secondAttributes, range: secondRange)
}