UITextView中的下划线文本

Jam*_*son 11 iphone xcode objective-c uitextview ios

如何在文本中加下划线UITextView.我明白我需要创建一个子类UITextView,但是会发生什么drawRect:

谢谢.

iDe*_*Dev 23

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

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

有关NSAttributedString检查的更多信息,请参阅如何使用NSAttributedString?

例如: -

textView.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)

UITextView上的Apple文档,

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

attributedText:

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

@property(nonatomic,copy) NSAttributedString *attributedText
Run Code Online (Sandbox Code Playgroud)

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


Ray*_*Fix 8

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

@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}
Run Code Online (Sandbox Code Playgroud)


Ada*_*hns 7

如果这是静态文本,您可以在Interface Builder中为其加下划线.确保首先通过在下拉菜单中选择"属性"来创建"属性"文本:

在此输入图像描述