【问题标题】:How do I append two attributed strings with different fonts?如何附加两个具有不同字体的属性字符串?
【发布时间】:2015-11-15 11:41:15
【问题描述】:

在这里,我尝试在一个 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]

【问题讨论】:

  • 您能否提供更多有关问题的信息?或者你遇到了什么错误?
  • 我没有显示任何错误

标签: ios swift fonts uikit nsattributedstring


【解决方案1】:

这是demo,你可以试一试,其实你应该提供更多关于错误是什么或错误是什么的信息。

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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多