我正在尝试创建一个允许下划线的UIButton
。这是我当前的代码:
func underline() {
if let textRange = selectedRange {
let attributedString = NSMutableAttributedString(attributedString: textView.attributedText)
textView.textStorage.addAttributes([.underlineStyle : NSUnderlineStyle.single.rawValue], range: textRange)
}
}
目前正在下划线,但我遇到的问题是我需要检查当前文本是否已经下划线以及是否已删除下划线。我似乎无法使用NSMutableAttributedString
来解决这个问题。
我正在使用斜体UIButton
这样做:
func italic() {
if let textRange = selectedRange {
let attributedString = NSAttributedString(attributedString: textView.attributedText)
attributedString.enumerateAttribute(.font, in: textRange, options: []) { (font, range, pointee) in
let newFont: UIFont
if let font = font as? UIFont {
let fontTraits = font.fontDescriptor.symbolicTraits
if fontTraits.contains(.traitItalic) {
newFont = UIFont.systemFont(ofSize: font.pointSize, weight: .regular)
} else {
newFont = UIFont.systemFont(ofSize: font.pointSize).italic()
}
textView.textStorage.addAttributes([.font : newFont], range: textRange)
}
}
}
}
如何实现检查当前文本是否具有我的第一个功能的下划线属性的能力?
代码到目前为止:
func isUnderlined(attrText: NSAttributedString) -> Bool {
var contains: ObjCBool = false
attrText.enumerateAttributes(in: NSRange(location: 0, length: attrText.length), options: []) { (dict, range, value) in
if dict.keys.contains(.underlineStyle) {
contains = true
}
}
return contains.boolValue
}
func underline() {
if let textRange = selectedRange {
let attributedString = NSMutableAttributedString(attributedString: textView.attributedText)
switch self.isUnderlined(attrText: attributedString) {
case true:
print("true")
textView.textStorage.removeAttribute(.underlineStyle, range: textRange)
case false:
print("remove")
textView.textStorage.addAttributes([.underlineStyle : NSUnderlineStyle.single.rawValue], range: textRange)
}
}
}
要检查文本是否已经下划线,您可以在文本的属性上简单地运行 contains(_:)
,即。
func isUnderlined(attrText: NSAttributedString) -> Bool {
var contains: ObjCBool = false
attrText.enumerateAttributes(in: NSRange(location: 0, length: attrText.length), options: []) { (dict, range, value) in
if dict.keys.contains(.underlineStyle) {
contains = true
}
}
return contains.boolValue
}
示例:
let attrText1 = NSAttributedString(string: "This is an underlined text.", attributes: [.underlineStyle : NSUnderlineStyle.styleSingle.rawValue])
let attrText2 = NSAttributedString(string: "This is an underlined text.", attributes: [.font : UIFont.systemFontSize])
print(self.isUnderlined(attrText: attrText1)) //true
print(self.isUnderlined(attrText: attrText2)) //false
您可以根据需要在UITextView
中使用上述逻辑。
要删除属性,
1。首先必须是NSMutableAttributedString
。
2。然后要删除属性,请在属性字符串上使用removeAttribute(_:range:)
方法。
let attrText1 = NSMutableAttributedString(string: "This is an underlined text.", attributes: [.underlineStyle : NSUnderlineStyle.styleSingle.rawValue])
print(self.isUnderlined(attrText: attrText1)) //true
if self.isUnderlined(attrText: attrText1) {
attrText1.removeAttribute(.underlineStyle, range: NSRange(location: 0, length: attrText1.string.count))
}
print(self.isUnderlined(attrText: attrText1)) //false
处理textView
按钮点击
@IBAction func onTapButton(_ sender: UIButton) {
if let selectedTextRange = self.textView.selectedTextRange {
let location = self.textView.offset(from: textView.beginningOfDocument, to: selectedTextRange.start)
let length = self.textView.offset(from: selectedTextRange.start, to: selectedTextRange.end)
let range = NSRange(location: location, length: length)
self.textView.attributedText.enumerateAttributes(in: range, options: []) { (dict, range, value) in
if dict.keys.contains(.underlineStyle) {
self.textView.textStorage.removeAttribute(.underlineStyle, range: range)
} else {
self.textView.textStorage.addAttributes([.underlineStyle : NSUnderlineStyle.styleSingle.rawValue], range: range)
}
}
}
}