iOS下划线粗细设置指南 - 使用NSAttributedString自定义下划线
{"title":"iOS 没有 underlineThickness","description":"在iOS中,没有名为underlineThickness的属性。underlineThickness是指字体下划线的粗细程度,但在iOS开发中,并没有直接控制下划线粗细的属性。不过,您可以使用NSAttributedString来自定义文本的下划线样式,包括粗细程度。下面是一个示例代码:\n\nswift\nlet yourLabel = UILabel()\n\nlet text = "Your Text"\nlet attributedText = NSMutableAttributedString(string: text)\n\n// 设置下划线样式\nattributedText.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))\n\n// 设置下划线颜色\nattributedText.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.red, range: NSRange(location: 0, length: text.count))\n\n// 设置下划线粗细程度(通过修改NSStrokeWidthAttributeName的值)\nattributedText.addAttribute(NSAttributedString.Key.strokeWidth, value: NSNumber(value: -3.0), range: NSRange(location: 0, length: text.count))\n\nyourLabel.attributedText = attributedText\n\n\n在上面的示例代码中,我们创建了一个NSMutableAttributedString对象,并使用addAttribute方法来设置下划线样式、颜色以及粗细程度。最后,将NSMutableAttributedString对象赋值给UILabel的attributedText属性,以显示自定义的下划线样式。"}
原文地址: https://www.cveoy.top/t/topic/pKfz 著作权归作者所有。请勿转载和采集!