ios UIlabel 中的下划线文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2711297/
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 17:10:19  来源:igfitidea点击:

Underline text in UIlabel

iostextuikituilabelunderline

提问by semix

How can I underline a text that could be multiple lines of string? I find some people suggest UIWebView, but it is obviously too heavy a class for just text rendering.

如何在可能是多行字符串的文本下划线?我发现有些人建议使用 UIWebView,但它显然对于文本渲染来说太重了。

My thoughts was to figure out the start point and length of each string in each line. And draw a line under it accordingly.

我的想法是找出每行中每个字符串的起点和长度。并相应地在其下方画一条线。

I meet problems at how to figure out the length and start point for the string.

我在如何确定字符串的长度和起点时遇到问题。

I tried to use -[UILabel textRectForBounds:limitedToNumberOfLines:], this should be the drawing bounding rect for the text right? Then I have to work on the alignment? How can I get the start point of each line when it is center-justified and right justified?

我尝试使用-[UILabel textRectForBounds:limitedToNumberOfLines:],这应该是文本的绘图边界矩形,对吗?然后我必须在对齐上工作?当每条线居中对齐和右对齐时,如何获得每条线的起点?

回答by kovpas

You may subclass from UILabel and override drawRect method:

您可以从 UILabel 继承子类并覆盖 drawRect 方法:

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f); // RGBA
    CGContextSetLineWidth(ctx, 1.0f);

    CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
    CGContextAddLineToPoint(ctx, self.bounds.size.width, self.bounds.size.height - 1);

    CGContextStrokePath(ctx);

    [super drawRect:rect];  
}


UPD:
As of iOS 6 Apple added NSAttributedString support for UILabel, so now it's much easier and works for multiple lines:

UPD:
从 iOS 6 开始,Apple 为 UILabel 添加了 NSAttributedString 支持,因此现在它更容易并且适用于多行:

NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
myLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Test string" 
                                                         attributes:underlineAttribute];

If you still wish to support iOS 4 and iOS 5, I'd recommend to use TTTAttributedLabelrather than underline label manually. However if you need to underline one-line UILabel and don't want to use third-party components, code above would still do the trick.

如果您仍然希望支持 iOS 4 和 iOS 5,我建议您使用TTTAttributedLabel而不是手动下划线标签。但是,如果您需要在单行 UILabel 下划线并且不想使用第三方组件,上面的代码仍然可以解决问题。

回答by ytll21

In Swift:

在斯威夫特:

let underlineAttriString = NSAttributedString(string: "attriString",
                                          attributes: [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue])
label.attributedText = underlineAttriString

回答by Sana

This is what i did. It works like butter.

这就是我所做的。它的作用就像黄油。

1) Add CoreText.framework to your Frameworks.

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

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

2)在需要下划线标签的类中导入 <CoreText/CoreText.h> 。

3) Write the following code.

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];

回答by Paulo Ferreira

Use an attribute string:

使用属性字符串:

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

And then override the label - (void)drawTextInRect:(CGRect)aRect and render the text in something like:

然后覆盖标签 - (void)drawTextInRect:(CGRect)aRect 并以如下方式呈现文本:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
drawingRect = self.bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, drawingRect);
textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
CGPathRelease(path);
CFRelease(framesetter);
CTFrameDraw(textFrame, ctx);
CGContextRestoreGState(ctx);

Or better yet instead of overriding just use the OHAttributedLabelcreated by Olivier Halligon

或者更好的,而不是压倒一切只是使用的OHAttributedLabel创建由Olivier Halligon

回答by Guntis Treulands

I've combined some of provided answers, to create better (at least for my requirements) UILabel subclass, which supports:

我结合了一些提供的答案,以创建更好的(至少对于我的要求) UILabel 子类,它支持:

  • multiline text with various label bounds (text can be in the middle of label frame, or accurate size)
  • underline
  • strikeout
  • underline/strikeout line offset
  • text alignment
  • different font sizes
  • 具有各种标签边界的多行文本(文本可以在标签框的中间,或者精确的大小)
  • 强调
  • 三振出局
  • 下划线/删除线偏移
  • 文字对齐
  • 不同的字体大小

https://github.com/GuntisTreulands/UnderLineLabel

https://github.com/GuntisTreulands/UnderLineLabel

回答by karim

People, who do not want to subclass the view (UILabel/UIButton) etc... 'forgetButton' can be replace by any lable too.

不想继承视图 (UILabel/UIButton) 等的人们……“forgetButton”也可以被任何标签替换。

-(void) drawUnderlinedLabel {
    NSString *string = [forgetButton titleForState:UIControlStateNormal];
    CGSize stringSize = [string sizeWithFont:forgetButton.titleLabel.font];
    CGRect buttonFrame = forgetButton.frame;
    CGRect labelFrame = CGRectMake(buttonFrame.origin.x + buttonFrame.size.width - stringSize.width, 
            buttonFrame.origin.y + stringSize.height + 1 , 
            stringSize.width, 2);
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
    lineLabel.backgroundColor = [UIColor blackColor];
    //[forgetButton addSubview:lineLabel];
    [self.view addSubview:lineLabel];
}

回答by Jill Wong

NSString *tem =self.detailCustomerCRMCaseLabel.text;
if (tem != nil && ![tem isEqualToString:@""]) {
    NSMutableAttributedString *temString=[[NSMutableAttributedString alloc]initWithString:tem];
    [temString addAttribute:NSUnderlineStyleAttributeName
                      value:[NSNumber numberWithInt:1]
                      range:(NSRange){0,[temString length]}];
    self.detailCustomerCRMCaseLabel.attributedText = temString;
}

回答by Roman Solodyashkin

NSMutableAttributedString *text = [self.myUILabel.attributedText mutableCopy];
[text addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, text.length)];
self.myUILabel.attributedText = text;

回答by youssman

Another solution could be (since iOS 7) given a negative value to NSBaselineOffsetAttributeName, for example your NSAttributedStringcould be:

另一种解决方案可能是(自 iOS 7 起)为 赋予负值NSBaselineOffsetAttributeName,例如您NSAttributedString可能是:

NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:@"my text goes here'
                                                            attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Regular" size:12],
                                                                         NSForegroundColorAttributeName: [UIColor blackColor],
                                                                         NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), NSBaselineOffsetAttributeName: @(-3)}];

Hope this will help ;-)

希望这会有所帮助;-)

回答by nfinfu

You can create a custom label with name UnderlinedLabel and edit drawRect function.

您可以创建一个名为 UnderlinedLabel 的自定义标签并编辑 drawRect 函数。

#import "UnderlinedLabel.h"

@implementation UnderlinedLabel

- (void)drawRect:(CGRect)rect
{
   NSString *normalTex = self.text;
   NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
   self.attributedText = [[NSAttributedString alloc] initWithString:normalTex
                                                      attributes:underlineAttribute];

   [super drawRect:rect];
}