Swift 如何检查文本是否有下划线

Swift 如何检查文本是否有下划线,swift,nsattributedstring,Swift,Nsattributedstring,我正在努力确定UITextView中的某些选定文本是否有下划线。我可以很容易地用以下代码检查粗体、斜体等: let isItalic = textView.font!.fontDescriptor.symbolicTraits.contains(.traitItalic) 但是,我不知道如何检查下划线?我刚刚创建了一个示例项目,我认为您可以执行以下操作: class ViewController: UIViewController { @IBOutlet weak var textV

我正在努力确定UITextView中的某些选定文本是否有下划线。我可以很容易地用以下代码检查粗体、斜体等:

let isItalic = textView.font!.fontDescriptor.symbolicTraits.contains(.traitItalic)

但是,我不知道如何检查下划线?

我刚刚创建了一个示例项目,我认为您可以执行以下操作:

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let attrText1 = NSMutableAttributedString(string: "TestTest", attributes: [.foregroundColor : UIColor.systemTeal, .underlineStyle: NSUnderlineStyle.single.rawValue])
        
        let attrText2 = NSAttributedString(string: " - not underlined", attributes: [.foregroundColor : UIColor.red])
        
        attrText1.append(attrText2)
        
        textView.attributedText = attrText1
    }
    
    func isTextUnderlined(attrText: NSAttributedString?, in range: NSRange) -> Bool {
        guard let attrText = attrText else { return false }
        var isUnderlined = false
        
        attrText.enumerateAttributes(in: range, options: []) { (dict, range, value) in
            if dict.keys.contains(.underlineStyle) {
                isUnderlined = true
            }
        }
        
        return isUnderlined
    }
    
    
    @IBAction func checkButtonDidTap(_ sender: UIButton) {
        print(isTextUnderlined(attrText: textView.attributedText, in: textView.selectedRange))
    }
    
}
创建一个扩展以将
选定范围
作为
NSRange
获取:

extension UITextInput {
    var selectedRange: NSRange? {
        guard let range = selectedTextRange else { return nil }
        let location = offset(from: beginningOfDocument, to: range.start)
        let length = offset(from: range.start, to: range.end)
        return NSRange(location: location, length: length)
    }
}

我刚刚创建了一个示例项目,我认为您可以执行以下操作:

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let attrText1 = NSMutableAttributedString(string: "TestTest", attributes: [.foregroundColor : UIColor.systemTeal, .underlineStyle: NSUnderlineStyle.single.rawValue])
        
        let attrText2 = NSAttributedString(string: " - not underlined", attributes: [.foregroundColor : UIColor.red])
        
        attrText1.append(attrText2)
        
        textView.attributedText = attrText1
    }
    
    func isTextUnderlined(attrText: NSAttributedString?, in range: NSRange) -> Bool {
        guard let attrText = attrText else { return false }
        var isUnderlined = false
        
        attrText.enumerateAttributes(in: range, options: []) { (dict, range, value) in
            if dict.keys.contains(.underlineStyle) {
                isUnderlined = true
            }
        }
        
        return isUnderlined
    }
    
    
    @IBAction func checkButtonDidTap(_ sender: UIButton) {
        print(isTextUnderlined(attrText: textView.attributedText, in: textView.selectedRange))
    }
    
}
创建一个扩展以将
选定范围
作为
NSRange
获取:

extension UITextInput {
    var selectedRange: NSRange? {
        guard let range = selectedTextRange else { return nil }
        let location = offset(from: beginningOfDocument, to: range.start)
        let length = offset(from: range.start, to: range.end)
        return NSRange(location: location, length: length)
    }
}

我相信下划线不是字体特征的一部分,它必须是文本的属性。你可能会发现这个问题的答案很有用。我希望它能帮助你

我认为下划线不是字体特征的一部分,它必须是文本的属性。你可能会发现这个问题的答案很有用。我希望它能帮助你

“UITextView中的选定文本”它不是全部内容。顺便说一句,Swift是一种类型推断语言
NSAttributedString。Key
是多余的。谢谢@finebel-它工作得很好,返回得很好。我简单地称之为:let range=textView.selectedRange if(isTextUnderlined(attrText:textView.attributedText,in:range))==true{print(“文本带下划线”)//适当地更新UI}否则{print(“文本不带下划线”)//适当地更新UI}瞧,我现在可以根据结果更新我的UI。我将范围添加为一个变量,因为我需要根据返回值对范围执行其他操作。“UITextView中的选定文本”不是全部内容。顺便说一句,Swift是一种类型推断语言
NSAttributedString。Key
是多余的。谢谢@finebel-它工作得很好,返回得很好。我简单地称之为:let range=textView.selectedRange if(isTextUnderlined(attrText:textView.attributedText,in:range))==true{print(“文本带下划线”)//适当地更新UI}否则{print(“文本不带下划线”)//适当地更新UI}瞧,我现在可以根据结果更新我的UI。我将范围添加为一个变量,因为我需要根据返回值对范围执行一些其他操作。如果第一个hald带下划线,而不是第二个hald,该怎么办?你想要“真”还是“假”?它有下划线吗?如果第一个hald有下划线,但第二个没有下划线呢?你想要“真”还是“假”?有下划线吗?