gpt4 book ai didi

ios - UITextView – 无法在文本上设置下划线或删除线属性?

转载 作者:技术小花猫 更新时间:2023-10-29 10:29:54 32 4
gpt4 key购买 nike

每当我尝试在 UITextView 的 attributedText 上设置下划线或删除线属性时,我都会遇到运行时失败。其他属性,如字体和背景颜色没有问题。这是一个代码片段。我所拥有的只是一个带有单个 UITextView 的测试项目。我使用的是 iOS 11。

class ViewController: UIViewController {

@IBOutlet weak var myTextView: UITextView!

var myMutableString = NSMutableAttributedString(string: "")

override func viewDidLoad() {
super.viewDidLoad()

let string = "The cow jumped over the moon" as NSString
myMutableString = NSMutableAttributedString(string: string as String)

let underlineAttribute = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle]

myMutableString.addAttributes(underlineAttribute, range: string.range(of: "cow"))

myTextView.attributedText = myMutableString
}
}

这会导致文本消失并打印出一些错误:

2017-11-05 19:43:34.708830-0800 UITextView_Test[11771:907014]
-[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 2017-11-05 19:43:34.709351-0800 UITextView_Test[11771:907014] <NSATSTypesetter: 0x60800016be80>: Exception -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 raised during typesetting layout manager <NSLayoutManager: 0x6080001fe400>
1 containers, text backing has 28 characters
Currently holding 28 glyphs.
Glyph tree contents: 28 characters, 28 glyphs, 1 nodes, 64 node bytes, 64 storage bytes, 128 total bytes, 4.57 bytes per character,
4.57 bytes per glyph
Layout tree contents: 28 characters, 28 glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, 64 node bytes, 0 storage bytes, 64 total bytes, 2.29 bytes per character, 2.29 bytes per glyph, 0.00 laid glyphs per laid line fragment, 0.00 bytes per laid line fragment , glyph range {0 28}. Ignoring... 2017-11-05 19:43:34.709827-0800 UITextView_Test[11771:907014] -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 2017-11-05 19:43:34.710014-0800 UITextView_Test[11771:907014] <NSATSTypesetter: 0x60800016be80>: Exception -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x60c00004eb50 raised during typesetting layout manager <NSLayoutManager: 0x6080001fe400>
1 containers, text backing has 28 characters
Currently holding 28 glyphs.
Glyph tree contents: 28 characters, 28 glyphs, 1 nodes, 64 node bytes, 64 storage bytes, 128 total bytes, 4.57 bytes per character,
4.57 bytes per glyph
Layout tree contents: 28 characters, 28 glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, 64 node bytes, 0 storage bytes, 64 total bytes, 2.29 bytes per character, 2.29 bytes per glyph, 0.00 laid glyphs per laid line fragment, 0.00 bytes per laid line fragment , glyph range {0 28}. Ignoring...

最佳答案

您的下划线样式无效。更改此行:

let underlineAttribute = 
[NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle]

为此:

let underlineAttribute = 
[NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]

结果:

enter image description here

关于ios - UITextView – 无法在文本上设置下划线或删除线属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47129473/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com