8 uitextview nsattributedstring ios swift
下载我创建的示例
我正在添加一个链接到我的UITextView,如下所示:
let systemFont = self.text.font!
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: alertController.textFields![0].text!)!] as [String : Any]
let myAttributes2 = [ NSForegroundColorAttributeName: customGreen]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
// self.text.font = systemFont
Run Code Online (Sandbox Code Playgroud)
但插入后,它会将UITextView中的所有当前文本更改为相同的字体.
因此,例如,如果我有一些粗体文本和一些更多的文本是Italic,在我添加url(它是系统字体)后,它会将所有粗体/斜体文本重置为系统字体....
如果有人知道如何保留以前文本的当前字体,我真的很感激帮助.
如需进一步说明,请在下方发表评论!
非常感谢任何有帮助的人!
更新
我正在更改textDidChange中的文本
if text == " "
{
// let systemFont = self.text.font!
// let linkAttributes = [NSFontAttributeName : systemFont]
let attributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: self.text.font!] as [String : Any]
let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
self.text.textStorage.insert(attributedString, at: range.location)
let cursor = NSRange(location: self.text.selectedRange.location+1, length: 0)
textView.selectedRange = cursor
return false
}
Run Code Online (Sandbox Code Playgroud)
我有这样所以添加URL后我创建一个空格并重置字体,所以我不继续键入作为URL链接...可能是问题,但当我键入正常,并没有设置URL链接文本没有'在腾出空间的同时改变......
let urlName = "google"
@IBAction func btnPressed(_ sender: Any) {
let systemFont = UIFont.systemFont(ofSize: 11)
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: "https://www.google.com")!] as [String : Any]
let myAttributes2 = [NSForegroundColorAttributeName: UIColor.green]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
}
Run Code Online (Sandbox Code Playgroud)
如果您想以编程方式添加文本,这就是您应该更新 textView 的方式。在这种情况下,请勿使用 textViewDidChange(_:) 委托方法。
更新
我已从 Dropbox 下载了您的代码并对其进行了一些更改。例如,我在这里更改 viewDidLoad() 中的 textView 文本。我创建了两个常量,以便在代码中再次使用它们,fontAttr是带有名称和大小的原始字体,fontColorAttrBlue是原始字体颜色。
let fontAttr = UIFont(name: "Helvetica-Bold", size: 20)
let fontColorAttrBlue = UIColor.blue
override func viewDidLoad() {
super.viewDidLoad()
let attrString = NSMutableAttributedString(string: "Hello World, How are you?", attributes: [NSFontAttributeName : fontAttr!, NSForegroundColorAttributeName: fontColorAttrBlue ])
self.textView.delegate = self // you can set the delegate from the storyboard.
self.textView.attributedText = attrString
}
Run Code Online (Sandbox Code Playgroud)
self.textView.font = systemFont我已经从操作方法中删除了这行代码webLink(_ sender: AnyObject),这样它就不会改变textView的字体。
最后,在textView(_:shouldChangeTextIn:replacementText:)方法中,我不是检查用户是否输入了" "字符串,而是检查用户是否输入了任何字符串,并重用了我在开始时创建的字体属性,这样,如果用户在链接后输入任何类型的文本,将是原始文本,而不是分配给类似文本的文本。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text.characters.count > 0 {
let attributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName : self.fontAttr!, NSForegroundColorAttributeName: self.fontColorAttrBlue])
self.textView.textStorage.insert(attributedString, at: range.location)
let cursor = NSRange(location: self.textView.selectedRange.location+1, length: 0)
textView.selectedRange = cursor
return false
}
return true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2730 次 |
| 最近记录: |