使文本在 xcode 中加下划线

问题描述:

嘿,我正在制作一个应用程序,它要求文本视图文本的某个部分带有下划线.有没有一种简单的方法可以做到这一点,例如将其设为粗体和斜体,或者我必须制作并导入自定义字体?提前感谢您的帮助!

Hey I'm making an app that requires a certain part of a text view's text to be underlined. is there a simple way to do this like for making it bold and italics or must i make and import a custom font? thanks for the help in advance!

这就是我所做的.它的作用就像黄油一样.

This is what i did. It works like butter.

1) 将 CoreText.framework 添加到您的框架中.

1) Add CoreText.framework to your Frameworks.

2) 在需要下划线标签的类中导入.

2) import <CoreText/CoreText.h> in the class where you need underlined label.

3) 编写以下代码.

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"My Messages"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
                  value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
                  range:(NSRange){0,[attString length]}];
self.myMsgLBL.attributedText = attString;
self.myMsgLBL.textColor = [UIColor whiteColor];