Как изменить цвет для некоторого текста в текстовом представлении? - PullRequest
1 голос
/ 29 апреля 2019

Я сделал домашнее задание для жизненного цикла контроллеров представления. Я использовал вкладку для переключения контроллеров. А при переключении контроллеров метка текстового представления отображает методы жизненного цикла.

Теперь я хочу изменить цвет текста в текстовом представлении для выражений «Первый контроллер», «Второй контроллер», «Третий контроллер», чтобы цвета оставались неизменными независимо от того, на какой контроллер я переключаюсь.

https://youtu.be/Od_bFlaA-kY

Как это сделать?

import UIKit

class FirstViewController: UIViewController {
    @IBOutlet var firstTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        FromCodeToScreen.shared.printMessage(textView: firstTextView, viewController: self)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        FromCodeToScreen.shared.printMessage(textView: firstTextView, viewController: self)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        FromCodeToScreen.shared.printMessage(textView: firstTextView, viewController: self)
    }

    // And similar code in all of the other lifecycle methods
}

import UIKit

class FromCodeToScreen: NSObject {
    static let shared = FromCodeToScreen()
    private var arrayForData = [String]()

    private override init() {
        super.init()
    }

    func printMessage(textView: UITextView, viewController: UIViewController, function: String = #function) {
        arrayForData.append((viewController.title ?? "nil") + " - " + (function))
        let string = arrayForData.joined(separator: "\n")
        textView.text = string

        textViewScrollToBottom(textView)
    }
}

Ответы [ 3 ]

2 голосов
/ 29 апреля 2019

Измените массив строк arrayForData на NSAttributedString.

class FromCodeToScreen: NSObject {

    static let shared = FromCodeToScreen()
    private var arrayForData = [NSAttributedString]()

    private override init() {
        super.init()
    }
    func printMessage(textView: UITextView, viewController: UIViewController, function: String = #function) {

        let color = viewController.view.backgroundColor ?? .white
        let attStr = NSMutableAttributedString(string: viewController.title ?? "nil", attributes: [.foregroundColor : color])
        attStr.append(NSAttributedString(string: " - " + function + "\n"))
        arrayForData.append(attStr)
        textView.attributedText = arrayForData.reduce(into: NSMutableAttributedString()) { $0.append($1) }
        textViewScrollToBottom(textView)
    }
}

enter image description here

0 голосов
/ 29 апреля 2019

Попробуйте:

firstTextView.textColor = UIColor.red

Примените это к viewDidLoad () для каждого ViewController.

Измените цвет на любой другой.

0 голосов
/ 29 апреля 2019

Попробуйте

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