要调整iOS UITextView中下划线与文字的距离,可以使用NSMutableAttributedString来自定义文本样式。以下是一个示例代码:\n\n\nlet textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\nlet text = \"Hello World\"\n\nlet attributedString = NSMutableAttributedString(string: text)\nlet range = NSRange(location: 0, length: text.count)\n\nattributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)\nattributedString.addAttribute(.underlineColor, value: UIColor.blue, range: range)\n\n// 调整下划线与文字的距离\nattributedString.addAttribute(.underlineOffset, value: 5, range: range)\n\ntextView.attributedText = attributedString\n\n\n在上述代码中,我们创建了一个UITextView,并使用NSMutableAttributedString将文本样式应用到文本上。使用addAttribute方法,我们设置了下划线样式和颜色,并使用addAttribute方法和underlineOffset键来调整下划线与文字的距离。将设置好的NSMutableAttributedString赋值给UITextView的attributedText属性,就可以在UITextView中显示带有自定义下划线距离的文本了。

iOS UITextView Swift: 调整下划线与文字距离

原文地址: https://www.cveoy.top/t/topic/pKq0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录