Почему атрибуты NSAttributedString влияют на другие AttributedString? - PullRequest
1 голос
/ 09 апреля 2019

В этом случае я устанавливаю атрибут attributeText UILabel с комбинированной NSAttributedString с разными атрибутами для каждой строки, и по некоторым причинам некоторые атрибуты AttributedString влияют на другие AttributedStrings, но я думал, что их атрибуты привязаны к диапазону этой конкретной AttributedString , Я в основном ожидаю, что NSAttributedString автоматически устанавливает свой диапазон, когда я добавляю его в NSMutableAttributedString.

Я не прав?

В этом случае тени применяются к другим добавленным атрибутам AttributedStrings, в другом случае это жирный шрифт, который переопределяет все шрифты для следующих AttributedStrings. Странно.

enter image description here

Вот пример кода:

    import UIKit

class ViewController: UIViewController {
        @IBOutlet weak var textLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping

        let myShadow = NSShadow()
        myShadow.shadowBlurRadius = 3
        myShadow.shadowOffset = CGSize(width: 3, height: 3)
        myShadow.shadowColor = UIColor.gray

        let attributedText = NSMutableAttributedString(string: "")

        let fatString = NSMutableAttributedString(string: "Bold", attributes: [NSAttributedString.Key.foregroundColor : UIColor.green,
                                                                               NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 16),
                                                                               NSAttributedString.Key.backgroundColor : UIColor.lightGray,
                                                                               NSAttributedString.Key.shadow : myShadow])

        let normalString = NSAttributedString(string: "\n\nNormal", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
                                                                                 NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)])

        let italicString = NSAttributedString(string: "\n\nItalic", attributes: [NSAttributedString.Key.foregroundColor : UIColor.yellow,
                                                                                 NSAttributedString.Key.font : UIFont.italicSystemFont(ofSize: 16)])

        let otherFontString = NSAttributedString(string: "\n\nBold + Italic", attributes: [NSAttributedString.Key.foregroundColor : UIColor.gray,
                                                                                           NSAttributedString.Key.font : UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 16)])

        let normalString2 = NSAttributedString(string: "\n\nUnderlined", attributes: [NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue,
                                                                                         NSAttributedString.Key.foregroundColor : UIColor.red,
                                                                                         NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)])

        attributedText.append(fatString)
        attributedText.append(normalString)
        attributedText.append(italicString)
        attributedText.append(otherFontString)
        attributedText.append(normalString2)

        textLabel.attributedText = attributedText
    }    
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...