为什么我在 UITextView 中的字体在 swift 中无缘无故地改变了样式?
why does my font in UITextView changes style without a reason in swift?
我的 swift
应用程序中有一个 UITextView
,我正在 viewDidLoad
中设置字体:
let font = UIFont(name: "AppleSDGothicNeo-Light", size: 16.0)
myTextView.font = font
它工作正常,当我 运行 应用程序并在文本视图中写一些东西时,我看到:
现在,我有一种方法可以检查给定的文本并在其中查找并突出显示 hashtags
。方法如下:
func formatTextInTextView(textView: UITextView) {
textView.scrollEnabled = false
let selectedRange = textView.selectedRange
let text = textView.text
// This will give me an attributedString with the base text-style
let attributedString = NSMutableAttributedString(string: text)
let regex = try? NSRegularExpression(pattern: "#(\w+)", options: [])
let matches = regex!.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
for match in matches {
let matchRange = match.rangeAtIndex(0)
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 244/255, green: 137/255, blue: 0/255, alpha: 1.0), NSFontAttributeName: font!]
attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
}
textView.attributedText = attributedString
textView.selectedRange = selectedRange
textView.scrollEnabled = true
}
我将此方法添加到:
func textViewDidChange(textView: UITextView) {
formatTextInTextView(textView)
}
现在,对于每个用户的输入,我都在动态检查它是否是主题标签,如果是 - 将文本突出显示为橙色。至少理论上应该是这样。所以当启用该方法时会发生这种情况:
我一开始写文字:
(我觉得这是系统字体)
当我添加主题标签时:
它适用于主题标签,但文本的其余部分采用 - 看起来像是 - 默认样式。这里有什么问题? :|
您需要在最初创建属性字符串时指定所需的字体,而不仅仅是颜色不同的部分。
func formatTextInTextView(textView: UITextView) {
textView.scrollEnabled = false
let selectedRange = textView.selectedRange
let text = textView.text
let titleDict: NSDictionary = [NSFontAttributeName: font!]
// This will give me an attributedString with the desired font
let attributedString = NSMutableAttributedString(string: text, attributes: titleDict)
let regex = try? NSRegularExpression(pattern: "#(\w+)", options: [])
let matches = regex!.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
for match in matches {
let matchRange = match.rangeAtIndex(0)
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 244/255, green: 137/255, blue: 0/255, alpha: 1.0)]
attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
}
textView.attributedText = attributedString
textView.selectedRange = selectedRange
textView.scrollEnabled = true
}
我的 swift
应用程序中有一个 UITextView
,我正在 viewDidLoad
中设置字体:
let font = UIFont(name: "AppleSDGothicNeo-Light", size: 16.0)
myTextView.font = font
它工作正常,当我 运行 应用程序并在文本视图中写一些东西时,我看到:
现在,我有一种方法可以检查给定的文本并在其中查找并突出显示 hashtags
。方法如下:
func formatTextInTextView(textView: UITextView) {
textView.scrollEnabled = false
let selectedRange = textView.selectedRange
let text = textView.text
// This will give me an attributedString with the base text-style
let attributedString = NSMutableAttributedString(string: text)
let regex = try? NSRegularExpression(pattern: "#(\w+)", options: [])
let matches = regex!.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
for match in matches {
let matchRange = match.rangeAtIndex(0)
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 244/255, green: 137/255, blue: 0/255, alpha: 1.0), NSFontAttributeName: font!]
attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
}
textView.attributedText = attributedString
textView.selectedRange = selectedRange
textView.scrollEnabled = true
}
我将此方法添加到:
func textViewDidChange(textView: UITextView) {
formatTextInTextView(textView)
}
现在,对于每个用户的输入,我都在动态检查它是否是主题标签,如果是 - 将文本突出显示为橙色。至少理论上应该是这样。所以当启用该方法时会发生这种情况:
我一开始写文字:
(我觉得这是系统字体)
当我添加主题标签时:
它适用于主题标签,但文本的其余部分采用 - 看起来像是 - 默认样式。这里有什么问题? :|
您需要在最初创建属性字符串时指定所需的字体,而不仅仅是颜色不同的部分。
func formatTextInTextView(textView: UITextView) {
textView.scrollEnabled = false
let selectedRange = textView.selectedRange
let text = textView.text
let titleDict: NSDictionary = [NSFontAttributeName: font!]
// This will give me an attributedString with the desired font
let attributedString = NSMutableAttributedString(string: text, attributes: titleDict)
let regex = try? NSRegularExpression(pattern: "#(\w+)", options: [])
let matches = regex!.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
for match in matches {
let matchRange = match.rangeAtIndex(0)
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 244/255, green: 137/255, blue: 0/255, alpha: 1.0)]
attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange)
}
textView.attributedText = attributedString
textView.selectedRange = selectedRange
textView.scrollEnabled = true
}