要调整iOS UITextView中下划线与文字的距离,可以使用NSMutableAttributedString来自定义文本样式。以下是一个示例代码:

let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
let text = "Hello World"

let attributedString = NSMutableAttributedString(string: text)
let range = NSRange(location: 0, length: text.count)

attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
attributedString.addAttribute(.underlineColor, value: UIColor.blue, range: range)

// 调整下划线与文字的距离
attributedString.addAttribute(.underlineOffset, value: 5, range: range)

textView.attributedText = attributedString

在上述代码中,我们创建了一个UITextView,并使用NSMutableAttributedString将文本样式应用到文本上。使用addAttribute方法,我们设置了下划线样式和颜色,并使用addAttribute方法和underlineOffset键来调整下划线与文字的距离。将设置好的NSMutableAttributedString赋值给UITextView的attributedText属性,就可以在UITextView中显示带有自定义下划线距离的文本了

ios uitextview siwft 下划线距离文字

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

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