How to check is UITextView is underlined or not iOS swift

Solution:

You cannot have any functions to check that.But you can test it by using the below method


    func attributes(value:Int) ->NSAttributedString {
        let textV = self.TextView
        let newTextView:UITextView! = UITextView.init()
        var attrs = [NSFontAttributeNameself.TextView.font,
                     NSForegroundColorAttributeNameself.TextView.textColor,
                     NSUnderlineStyleAttributeName : value] as! [String : Any]
        var attributedString = NSMutableAttributedString(string:"")
        let buttonTitleStr = NSMutableAttributedString(string:(self.TextView.text)!, attributes:attrs)
        attributedString.append(buttonTitleStr)
        newTextView.attributedText = attributedString
        return newTextView.attributedText

    }


In your textview you can check it by using the below code

            let attributedString = self.attributes(value: 1)

            if attributedString == self.TextView.attributedText! {
                
                self.TextView.attributedText = self.attributes(value: 0)
                btn.isSelected = false
                btn.backgroundColor = UIColor.clear
            } else {
                self.TextView.attributedText = attributedString
                btn.isSelected = true
                btn.backgroundColor = UIColor.white

            }

self.TextView is your textview that you want to check the underline

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift