UITextView中的下划线文本 [英] Underline text in a UITextView
问题描述
如何在 UITextView
中为文本加下划线。我知道我需要创建一个 UITextView
的子类,但是在 drawRect:
下会有什么?
How can I underline text in a UITextView
. I understand that I would need to create a subclass of UITextView
, but what would go under drawRect:
?
谢谢。
推荐答案
尝试使用 NSAttributedString
如下所示,并在 UITextView
中设置。这适用于iOS6。
Try to use NSAttributedString
as follows and set in UITextView
. This works for iOS6.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
range:(NSRange){0,[attString length]}];
有关 NSAttributedString
的更多信息,请检查< a href =https://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring>你如何使用NSAttributedString?
For more info on NSAttributedString
check this How do you use NSAttributedString?
例如: -
textView.attributedText = attString;
来自 UITextView上的苹果文档,
在iOS 6及更高版本中,此类通过
使用attributedText属性支持多种文本样式。 (
早期版本的iOS不支持样式化文本。)为此属性设置值会导致
文本视图使用属性
字符串中提供的样式信息。您仍然可以使用font,textColor和textAlignment
属性来设置样式属性,但这些属性适用于文本视图中文本的所有
。
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.
文本视图显示的样式文本。
attributedText:
The styled text displayed by the text view.
@property(nonatomic,copy) NSAttributedString *attributedText
讨论:默认情况下,此属性为零。为此属性分配新值也会使用相同的字符串数据替换text属性的值,尽管没有任何格式设置信息。此外,分配新的值会更新font,textColor和textAlignment属性中的值,以便它们反映从属性字符串中的位置0开始的样式信息。
Discussion: This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and textAlignment properties so that they reflect the style information starting at location 0 in the attributed string.
这篇关于UITextView中的下划线文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!