Как добавить две приписанные строки с разными шрифтами?

0

Здесь я попытался добавить две UILabel строки в одном 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
    Можете ли вы предоставить больше информации о том, что не так? Или какую ошибку вы получаете?
  • 1
    Это не показывает никакой ошибки на моем конце
Теги:
fonts
uikit
nsattributedstring

1 ответ

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)
}
  • 1
    Это работает хорошо, спасибо @ Сиам ...

Ещё вопросы

Сообщество Overcoder
Наверх
Меню