Ios 同一UITextView中的String和AttAttributedString
我可以在同一Ios 同一UITextView中的String和AttAttributedString,ios,swift,string,uitextview,nsmutableattributedstring,Ios,Swift,String,Uitextview,Nsmutableattributedstring,我可以在同一UITextView中使用字符串和NSMutableAttributedString吗 我正在导入一个.docx文件,并将其转换为字符串,然后在UITextField中显示该文件,但我希望为特定的单词着色。理想情况下,用户会键入“LineBreak”,它会自动将单词LineBreak更改为不同的颜色 据我所知,这将需要使用NSMutableAttributedString,但我不知道如何执行此操作 let string = "Test Specific Colour LineBrea
UITextView
中使用字符串和NSMutableAttributedString
吗
我正在导入一个.docx
文件,并将其转换为字符串
,然后在UITextField
中显示该文件,但我希望为特定的单词着色。理想情况下,用户会键入“LineBreak”,它会自动将单词LineBreak更改为不同的颜色
据我所知,这将需要使用NSMutableAttributedString
,但我不知道如何执行此操作
let string = "Test Specific Colour LineBreak TestLine2"
let attributedString = NSMutableAttributedString.init(string: string)
let range = (string as NSString).range(of: "LineBreak")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
value: UIColor.blue, range: range)
txtView.attributedText = attributedString
因此,使用上面的例子,我想让“LineBreak”的颜色在任何时候都改变。上面的操作可以改变颜色,但不是每次我打字都可以。我需要识别字符串“LineBreak”的存在并更改其颜色
实现我所追求的目标的最佳方式是什么?这里有一种实现你想要的目标的方式
// set your textview delegate in your view controller if you are using textview
// set the target if you are using textfield
// textfield.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
class ViewController: UIViewController,UITextViewDelegate {
@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var txtView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtView.delegate = self
textfield.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
// Do any additional setup after loading the view.
}
// if you are using textview implement this
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let string = textView.text
if textView.text.contains("LineBreak") {
let attributedString = NSMutableAttributedString.init(string: textView.text)
let range = (string as! NSString).range(of: "LineBreak")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
value: UIColor.blue, range: range)
textView.attributedText = attributedString
}
}
// if you are using textfield you need to
// add this line in your didload
//textfield.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
// and implement this function
@objc func textFieldDidChange() {
let string = textfield.text
if textfield.text!.contains("LineBreak") {
let attributedString = NSMutableAttributedString.init(string: textfield.text!)
let range = (string as! NSString).range(of: "LineBreak")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
value: UIColor.blue, range: range)
textfield.attributedText = attributedString
}
}
}
输出:
这里有一种方法可以实现你想要的
// set your textview delegate in your view controller if you are using textview
// set the target if you are using textfield
// textfield.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
class ViewController: UIViewController,UITextViewDelegate {
@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var txtView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtView.delegate = self
textfield.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
// Do any additional setup after loading the view.
}
// if you are using textview implement this
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let string = textView.text
if textView.text.contains("LineBreak") {
let attributedString = NSMutableAttributedString.init(string: textView.text)
let range = (string as! NSString).range(of: "LineBreak")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
value: UIColor.blue, range: range)
textView.attributedText = attributedString
}
}
// if you are using textfield you need to
// add this line in your didload
//textfield.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
// and implement this function
@objc func textFieldDidChange() {
let string = textfield.text
if textfield.text!.contains("LineBreak") {
let attributedString = NSMutableAttributedString.init(string: textfield.text!)
let range = (string as! NSString).range(of: "LineBreak")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
value: UIColor.blue, range: range)
textfield.attributedText = attributedString
}
}
}
输出:
这里有一种方法可以让您开始
确保控制器符合UITextViewDelegate
,并且已分配文本视图的委托:
class ExampleViewController: UIViewController, UITextViewDelegate {
@IBOutlet var theTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
theTextView.delegate = self
theTextView.text = "Test Specific Colour LineBreak TestLine2 linebreak and another LineBreak occurance"
formatTextInTextView(theTextView)
}
func textViewDidChange(_ textView: UITextView) {
formatTextInTextView(textView)
}
func formatTextInTextView(_ textView: UITextView) -> Void {
guard let curText = textView.text else { return }
let bScroll = textView.isScrollEnabled
textView.isScrollEnabled = false
let selRange = textView.selectedRange
let attributedString = NSMutableAttributedString.init(string: curText)
let regex = try! NSRegularExpression(pattern: "LineBreak", options: [.caseInsensitive])
let items = regex.matches(in: curText, options: [], range: NSRange(location: 0, length: curText.count))
let ranges: [NSRange] = items.map{$0.range}
for r in ranges {
attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: r)
}
textView.attributedText = attributedString
textView.selectedRange = selRange
textView.isScrollEnabled = bScroll
}
}
您可能希望将其限制为区分大小写、全字等。这里有一种方法可以帮助您开始
确保控制器符合UITextViewDelegate
,并且已分配文本视图的委托:
class ExampleViewController: UIViewController, UITextViewDelegate {
@IBOutlet var theTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
theTextView.delegate = self
theTextView.text = "Test Specific Colour LineBreak TestLine2 linebreak and another LineBreak occurance"
formatTextInTextView(theTextView)
}
func textViewDidChange(_ textView: UITextView) {
formatTextInTextView(textView)
}
func formatTextInTextView(_ textView: UITextView) -> Void {
guard let curText = textView.text else { return }
let bScroll = textView.isScrollEnabled
textView.isScrollEnabled = false
let selRange = textView.selectedRange
let attributedString = NSMutableAttributedString.init(string: curText)
let regex = try! NSRegularExpression(pattern: "LineBreak", options: [.caseInsensitive])
let items = regex.matches(in: curText, options: [], range: NSRange(location: 0, length: curText.count))
let ranges: [NSRange] = items.map{$0.range}
for r in ranges {
attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: r)
}
textView.attributedText = attributedString
textView.selectedRange = selRange
textView.isScrollEnabled = bScroll
}
}
您可能希望将其限制为区分大小写、全字等。向我们展示您在那里尝试的内容,它可能会有用:@Was'siimbenhsen,请参阅更新question@elevate答案对你有帮助吗?告诉我们你在那里尝试了什么,它可能有用:@was'siimbenhsen,请参阅更新question@elevate这个答案对你有帮助吗?