Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Swift在注释中的字符下划线_Swift_String_Text_Character_Comments - Fatal编程技术网

如何使用Swift在注释中的字符下划线

如何使用Swift在注释中的字符下划线,swift,string,text,character,comments,Swift,String,Text,Character,Comments,例如,我正在编写一个函数,我想显示一个add方法。我想这样强调上面的评论 // This is the comment I want to underline // Adding a few other things you can do with comments would also be helpful func helpMeUnderlineThisComment(comment: String) -> String { for char in comment {

例如,我正在编写一个函数,我想显示一个add方法。我想这样强调上面的评论

// This is the comment I want to underline 
// Adding a few other things you can do with comments would also be helpful

func helpMeUnderlineThisComment(comment: String) -> String {
     for char in comment {
        if char == comment {
          return char.underlined()
        }
     }
     return nil
}

您只需做一个扩展即可:

extension NSAttributedString {
var underlined: NSAttributedString {
    return applying(attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
}

func applying(attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
    let copy = NSMutableAttributedString(attributedString: self)
    let range = (string as NSString).range(of: string)
    copy.addAttributes(attributes, range: range)
    return copy
}
}
如何使用它

class ViewController: UIViewController {
@IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.textLabel.text = "Some Text"
    self.textLabel.attributedText = textLabel.attributedText?.underlined
}}

[检查这个。]()它对你有用吗?@AbhishekPatel我的意思是如何在注释下面划线,对不起--忽略代码。