How to change color for some text in text view?

Related searches

I made my homework for life cycle of view controllers. I used a tabbar to switch controllers. And when switching controllers, the label of text view to display life-cycle methods.

Now I want to change color text in text view for expression "First Controller", "Second Controller", "Third Controller" so that the colors remain the same regardless of which controller I switch to.

https://youtu.be/Od_bFlaA-kY

How to do it?

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

Change arrayForData string array to 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)
    }
}

How to change text color of the part of the TextView?, Try This: Set TextView as a HTML using SpannableTextView. String text = "<font color='black'>123456</font><font color='red'>789</font>";� This method uses a divergent color palette to color text based on whether it is over or under a threshold. These directions assume the view is already built. For an example with directions from scratch, see the "Opt 1: Divergent Color Palette" worksheet in the workbook in the right-hand pane of this article.

Try This

let text = "This is a colorful attributed string"
let attributedText = 
NSMutableAttributedString.getAttributedString(fromString: text)
attributedText.apply(color: .red, subString: "This")
//Apply yellow color on range
attributedText.apply(color: .yellow, onRange: NSMakeRange(5, 4))


For more detail click here: https://github.com/iOSTechHub/AttributedString


How to Change the Text Color of a Substring, together with ForegroundColorSpan and BackGroundColorSpan to change the text color of Duration: 6:35 Posted: Mar 19, 2018 Text Color In HTML, you can change the text color using Cascading Style Sheets (CSS). You can change the text color using inline style sheets, embedded style sheets, or external style sheets. For the purposes of these examples, we'll use inline style sheets, however, I encourage you to use external style sheets whereever possible.

Try:

firstTextView.textColor = UIColor.red

Apply this on viewDidLoad() for each ViewController.

Change the color to whatever you want.

Spantastic text styling with Spans | by Florina Muntenescu, Change the color of a few characters, make them clickable, scale the span, and the rest of the text, the colour set by the TextView attribute. To start with you should add a new script to your text. This code changes the colour when entering and exiting the text object:

How to change a color of text and background in Android TextView?, Set a background color in XML and Java – android:background and setBackgroundColor (). There isn't any dedicated XML tag attribute for just� Outlook allows you to change the font, color, size, and style for new messages and replies and forwards independently. If you want to change the font used in the message list, see Create, change, or customize a view. Change the default font, color, style, and size for messages. On the File tab, choose Options > Mail.

Lesson 1.5: How to color part of the text and use alpha channel in , Then we give a result of HTML parsing to setText() method, which displays it in chosen TextView. Of course we have to find a view first, what was� // Specify the layout you are using. setContentView(R.layout.yourlayout) // Load and use views afterwards val tv1: TextView = findViewById(R.id.textView1) tv1.text = "Hello" Click Here to study exactly you want to know

For now there is no option to change the text message bubble color or change or change the message background. Maybe Samsung may add those features in upcoming Software updates. We'll just have to wait and see.

Comments
  • You are looking for NSAttributedString.
  • But I need different colors for text "First Controller, "Second Controller, "Third Controller", For example First Controller - red, Second - violet, Third - green ! and when I switch between them, the colors are displayed which I set
  • @alexleo When you are in first view controller the whole text should be in red? and when you switch to second view controller the whole text should be in violet?
  • @alexleo if so just set firstTextView.textColor = .red in first view controller...
  • No! not whole, I want that the words "First Controller" to be red on all screens to which I switch, so that the words "Second Controller" are also green in all screens and the same for words "Third Controller" - violet color
  • @alexleo and all function names in black color?
  • Is this a universal solution for all Controllers or I need apply this for each(I have 3) ?
  • It's specific to one view controller. You can apply it to all of them in one of several ways: 1) Put the logic in a base view controller and make your 3 view controllers subclass it; 2) Subclass UITextView to apply the changes when its text attribute is set, and then use the subclassed text view in all 3 view controllers; 3) put this in a utility function that all 3 view controllers can call; etc.
  • Have you tried your answer on a UITextView with NSAttributedStrings?