如何使用Swift在注释中的字符下划线
例如,我正在编写一个函数,我想显示一个add方法。我想这样强调上面的评论如何使用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 {
// 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我的意思是如何在注释下面划线,对不起--忽略代码。