Swift 如何在文本视图中更改某些文本的颜色?
我做了视图控制器生命周期的家庭作业。我使用一个选项卡来切换控制器。切换控制器时,文本视图的标签显示生命周期方法 现在我想在文本视图中更改表达式“第一个控制器”、“第二个控制器”、“第三个控制器”的颜色文本,以便无论切换到哪个控制器,颜色都保持不变 怎么做Swift 如何在文本视图中更改某些文本的颜色?,swift,uitextview,Swift,Uitextview,我做了视图控制器生命周期的家庭作业。我使用一个选项卡来切换控制器。切换控制器时,文本视图的标签显示生命周期方法 现在我想在文本视图中更改表达式“第一个控制器”、“第二个控制器”、“第三个控制器”的颜色文本,以便无论切换到哪个控制器,颜色都保持不变 怎么做 import UIKit class FirstViewController: UIViewController { @IBOutlet var firstTextView: UITextView! override fu
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
试试这个
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()) { $0.append($1) }
textViewScrollToBottom(textView)
}
}
将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)
}
}
试试:
firstTextView.textColor = UIColor.red
将此应用于每个ViewController的viewDidLoad()
将颜色更改为您想要的任何颜色。尝试:
firstTextView.textColor = UIColor.red
将此应用于每个ViewController的viewDidLoad()
将颜色更改为您想要的颜色。您正在查找
NSAttributedString
。您正在查找NSAttributedString
。这是所有控制器的通用解决方案,还是我需要为每个控制器应用此解决方案(我有3个)?它特定于一个视图控制器。您可以通过以下几种方式之一将其应用于所有视图:1)将逻辑放在基本视图控制器中,并使3个视图控制器成为它的子类;2) 子类UITextView
在设置其text
属性时应用更改,然后在所有3个视图控制器中使用子类文本视图;3) 将其放入所有3个视图控制器都可以调用的实用程序函数中;等等。这是一个适用于所有控制器的通用解决方案,还是我需要将其应用于每个控制器(我有3个)?它特定于一个视图控制器。您可以通过以下几种方式之一将其应用于所有视图:1)将逻辑放在基本视图控制器中,并使3个视图控制器成为它的子类;2) 子类UITextView
在设置其text
属性时应用更改,然后在所有3个视图控制器中使用子类文本视图;3) 将其放入所有3个视图控制器都可以调用的实用程序函数中;等,但我需要不同的颜色文本“第一控制器”,第二控制器,第三控制器”,例如,第一个控制器-红色、第二个-紫色、第三个-绿色!当我在它们之间切换时,将显示我所选择的颜色set@alexleo当您在第一视图控制器中时,整个文本应为红色?当您切换到第二视图控制器时,整个文本应为紫色?@alexleo如果是,请设置firstTextView.textColor=.第一视图控制器中的红色…不!不是全部,我希望所有切换到的屏幕上的“第一个控制器”都是红色的,这样所有屏幕上的“第二个控制器”也都是绿色的,而“第三个控制器”也一样-紫色color@alexleo所有函数名都是黑色的?但我需要不同的文本颜色第一控制器,第二控制器,第三控制器,例如,第一个控制器-红色、第二个-紫色、第三个-绿色!当我在它们之间切换时,将显示我所选择的颜色set@alexleo当您在第一视图控制器中时,整个文本应为红色?当您切换到第二视图控制器时,整个文本应为紫色?@alexleo如果是,请设置firstTextView.textColor=.第一视图控制器中的红色…不!不是全部,我希望所有切换到的屏幕上的“第一个控制器”都是红色的,这样所有屏幕上的“第二个控制器”也都是绿色的,“第三个控制器”也一样-紫罗兰色color@alexleo所有函数名都是黑色的吗?您是否使用nsattributed string
s在UITextView
上尝试过答案?您是否使用nsattributed string
s在UITextView
上尝试过答案?