iPhone 中的文本下划线

发布于 2024-11-03 18:41:12 字数 62 浏览 1 评论 0原文

如何在 Objective C iPhone 中为文本添加下划线? UILabel的文本下划线有什么方法吗?

How to underline text in objective c iphone??
Is there any method for underline text of UILabel??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗笺 2024-11-10 18:41:12

子类 UILabel 并重写 drawRect 方法,如下所示。下划线将具有与标签相同的文本颜色和文本对齐方式:

- (void)drawRect:(CGRect)rect
{
    if([[self.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length])
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0); // RGBA
        CGContextSetLineWidth(ctx, 1.0f);
        CGSize tmpSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(200, 9999)];

        // check text alignment
        if(self.textAlignment == UITextAlignmentLeft) {
            CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width, self.bounds.size.height - 1);
        }else if(self.textAlignment == UITextAlignmentCenter) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width) / 2;
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width + startPoint, self.bounds.size.height - 1);
        }else if (self.textAlignment == UITextAlignmentRight) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width);
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, self.frame.size.width, self.bounds.size.height - 1);
        }

        CGContextStrokePath(ctx);
    }
    [super drawRect:rect];
}

Subclass UILabel and override drawRect method as below. Underline will have the same text color and text alignment as the label:

- (void)drawRect:(CGRect)rect
{
    if([[self.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length])
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0); // RGBA
        CGContextSetLineWidth(ctx, 1.0f);
        CGSize tmpSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(200, 9999)];

        // check text alignment
        if(self.textAlignment == UITextAlignmentLeft) {
            CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width, self.bounds.size.height - 1);
        }else if(self.textAlignment == UITextAlignmentCenter) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width) / 2;
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width + startPoint, self.bounds.size.height - 1);
        }else if (self.textAlignment == UITextAlignmentRight) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width);
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, self.frame.size.width, self.bounds.size.height - 1);
        }

        CGContextStrokePath(ctx);
    }
    [super drawRect:rect];
}
柠檬色的秋千 2024-11-10 18:41:12

您可以从 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];  
}

You may subclass from UILabel and override drawRect method:

- (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];  
}
一曲爱恨情仇 2024-11-10 18:41:12

简而言之,UILabel 中没有可用的高级格式。

如果您要寻找的是链接,那么您最好使用 UIWebView,并为其提供一些“自制 HTML”,例如“我的链接”。然后,您可以在 Web 视图的委托中处理 Web 视图上的点击。

Simply put, there's no advanced formatting available in UILabel.

If what you're looking for is a link, then you're better of using an UIWebView, and feed it some "home-made HTML", like "My Link". Then you can handle click on the webview in your webview's delegate.

情绪少女 2024-11-10 18:41:12

使用属性字符串:

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

然后覆盖标签 - (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);

或者更好的是,不要覆盖,只需使用 OHAttributedLabel 由 Olivier Halligon 创建。他还支持自定义链接及其自定义颜色。

Use an attribute string:

NSAttributedString* attrString = [[NSAttributedString 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:

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 OHAttributedLabel created by Olivier Halligon. He also has support for custom links, and for custom colors for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文