iPhone 中的文本下划线

3

如何在Objective-C iPhone中给文本添加下划线?是否有一种方法可以在UILabel中给文本添加下划线?


可能是重复的问题 https://dev59.com/_3E85IYBdhLWcg3wkkbK - Jhaliya - Praveen Sharma
在提问之前请先搜索。 - Inder Kumar Rathore
4个回答

3

继承 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];
}

2

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

谢谢...但我也想在这个页面上放链接。 - Nirav Jain
如果您想要一个链接,为什么不使用自定义按钮,将标题作为下划线文本,并使用按钮操作打开链接呢? - visakh7
您甚至可以尝试在标签上方放置一个自定义的透明按钮。 - visakh7

1

简单来说,在UILabel中没有可用的高级格式。

如果你想要的是一个链接,那么最好使用UIWebView,并将一些“自制HTML”提供给它,比如“My Link”。然后你可以在你的webview代理中处理对webview的点击。


0
使用属性字符串:
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);

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接