如何在文本视图中更改某些文本的颜色?
How to change color for some text in text view?
我为视图控制器的生命周期做了功课。我使用标签栏来切换控制器。以及切换控制器时,文本视图标签显示生命周期方法。
现在我想更改文本视图中表达式 "First Controller"、"Second Controller"、"Third Controller" 的颜色文本,以便无论我切换到哪个控制器,颜色都保持不变。
怎么做?
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)
}
}
试试这个
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
将 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()) { [=10=].append() }
textViewScrollToBottom(textView)
}
}
尝试:
firstTextView.textColor = UIColor.red
在 viewDidLoad() 上为每个 ViewController 应用这个。
将颜色更改为您想要的任何颜色。
我为视图控制器的生命周期做了功课。我使用标签栏来切换控制器。以及切换控制器时,文本视图标签显示生命周期方法。
现在我想更改文本视图中表达式 "First Controller"、"Second Controller"、"Third Controller" 的颜色文本,以便无论我切换到哪个控制器,颜色都保持不变。
怎么做?
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)
}
}
试试这个
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
将 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()) { [=10=].append() }
textViewScrollToBottom(textView)
}
}
尝试:
firstTextView.textColor = UIColor.red
在 viewDidLoad() 上为每个 ViewController 应用这个。
将颜色更改为您想要的任何颜色。