gpt4 book ai didi

iphone - UITextView 中的下划线文本

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

如何在 UITextView 中为文本添加下划线。我知道我需要创建 UITextView 的子类,但是 drawRect: 下会发生什么?

谢谢。

最佳答案

尝试如下使用NSAttributedString,并在UITextView中设置。这适用于 iOS6。

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
range:(NSRange){0,[attString length]}];

有关 NSAttributedString 的更多信息,请查看 How do you use NSAttributedString?

例如:-

textView.attributedText = attString;

来自 apple documentation on UITextView ,

In iOS 6 and later, this class supports multiple text styles through use of the attributedText property. (Styled text is not supported in earlier versions of iOS.) Setting a value for this property causes the text view to use the style information provided in the attributed string. You can still use the font, textColor, and textAlignment properties to set style attributes, but those properties apply to all of the text in the text view.

属性文本:

TextView 显示的样式文本。

@property(nonatomic,copy) NSAttributedString *attributedText

讨论: 该属性默认为 nil。为这个属性分配一个新值也会用相同的字符串数据替换文本属性的值,尽管没有任何格式信息。此外,分配新值会更新 font、textColor 和 textAlignment 属性中的值,以便它们反射(reflect)从属性字符串中位置 0 开始的样式信息。

关于iphone - UITextView 中的下划线文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112965/

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