在 iOS UITextView 中,可以使用 NSAttributedString 来设置下划线的样式和属性。然而,在 Swift 中,并没有直接提供 NSAttributedString.Key.underlineOffset 这个属性来设置下划线和文字之间的距离。

不过,你可以通过自定义 UITextView 的子类来实现这个效果。下面是一个示例代码:

class CustomTextView: UITextView {
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        if let text = self.text {
            let attributedString = NSMutableAttributedString(string: text)
            
            // 设置下划线的样式和属性
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))
            attributedString.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.red, range: NSRange(location: 0, length: text.count))
            
            // 设置下划线和文字之间的距离
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 10 // 设置行间距,即下划线和文字之间的距离
            attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: text.count))
            
            self.attributedText = attributedString
        }
    }
}

在上面的示例代码中,我们创建了一个自定义的 UITextView 子类 CustomTextView,并重写了 draw(_ rect: CGRect) 方法。在这个方法中,我们先调用了父类的 draw(_ rect: CGRect) 方法,然后根据需求设置了下划线的样式和属性,以及下划线和文字之间的距离。

你可以使用这个 CustomTextView 类来替代原来的 UITextView,并调用其相应的方法和属性来设置下划线的样式和属性,以及下划线和文字之间的距离。

注意:由于 UITextView 是一个复杂的控件,可能还需要进行其他的调整和处理,以满足你的具体需求

ios uitextview siwft 下划线距离文字没有NSAttributedStringKeyunderlineOffset

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

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