ios UITextView 中的下划线文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13112965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:58:11  来源:igfitidea点击:

Underline text in a UITextView

iphoneobjective-ciosxcodeuitextview

提问by James Anderson

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:?

如何在UITextView. 我知道我需要创建一个 的子类UITextView,但是下面会发生什么drawRect:

Thanks.

谢谢。

回答by iDev

Try to use NSAttributedStringas follows and set in UITextView. This works for iOS6.

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

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

For more info on NSAttributedStringcheck this How do you use NSAttributedString?

有关NSAttributedString检查此如何使用 NSAttributedString 的更多信息

For eg:-

例如:-

textView.attributedText = attString;

From apple documentation on UITextView,

关于 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.

在 iOS 6 及更高版本中,此类通过使用 attributesText 属性支持多种文本样式。(iOS 的早期版本不支持样式文本。)为此属性设置值会导致文本视图使用属性字符串中提供的样式信息。您仍然可以使用 font、textColor 和 textAlignment 属性来设置样式属性,但这些属性适用于文本视图中的所有文本。

attributedText:

属性文本:

The styled text displayed by the text view.

文本视图显示的样式文本。

@property(nonatomic,copy) NSAttributedString *attributedText

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.

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

回答by Ray Fix

If you want to avoid having to include CoreText, you can utilize an attributed string with this attribute:

如果您想避免必须包含 CoreText,您可以使用具有此属性的属性字符串:

@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}

回答by Adam Johns

If this is static text, you can underline it in Interface Builder. Make sure to make the text 'Attributed' first by selecting 'Attributed' in the drop down menu:

如果这是静态文本,您可以在 Interface Builder 中给它加下划线。确保首先通过在下拉菜单中选择“归因”来使文本“归因”:

enter image description here

在此处输入图片说明

回答by jose920405

textViewMessage.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]};

回答by Tayo119

You can't use "kCTUnderlineStyleAttributeName" or "kCTUnderlineStyleSingle" Now you must do it like this:

你不能使用 "kCTUnderlineStyleAttributeName" 或 "kCTUnderlineStyleSingle" 现在你必须这样做:

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Text"];
[attString addAttribute:NSUnderlineStyleAttributeName
                  value:@(NSUnderlineStyleSingle)
                  range:(NSRange){0,[attString length]}];

回答by Solidus

If you want to format your text (with underlined words, links, colored words...) I suggest you to use FTCoreText

如果你想格式化你的文本(带下划线的词,链接,彩色的词......)我建议你使用FTCoreText

回答by user3651677

-(IBAction)underline:(id)sender
{
    NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
    texts.attributedText = [[NSAttributedString alloc] initWithString:texts.text
                                                           attributes:underlineAttribute];
}

回答by rmaddy

If you are using iOS 6 then you can use the attributedTextattribute of UITextView. Apply underline formatting to the text. You can also set the typingAttributesproperty to ensure the text that the user types has a specific set of formatting if you wish.

如果您使用的是 iOS 6,那么您可以使用UITextViewattributedText属性。对文本应用下划线格式。如果您愿意,您还可以设置该属性以确保用户键入的文本具有一组特定的格式。typingAttributes

回答by Mathew Varghese

I recommend you to use CoreText. A Basic tutorial is here on raywenderlich.

我建议您使用CoreTextraywenderlich 上有一个基本教程。

回答by yebw

I recommend you to use MFUnderlinedTextView, it will be helpful.

我建议您使用MFUnderlinedTextView,它会有所帮助。