【问题标题】:Text Underline in iphoneiphone中的文字下划线
【发布时间】:2011-08-12 06:20:15
【问题描述】:

如何在objective c iphone中给文字加下划线?? UILabel的文字下划线有什么方法吗??

【问题讨论】:

标签: objective-c iphone ipad underline


【解决方案1】:

子类UILabel 并覆盖drawRect 方法如下。下划线将以same text colortext alignment 作为标签:

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

    【讨论】:

    • 谢谢...但我也想要这个页面上的链接。
    • 如果你想要一个链接,为什么不使用标题作为下划线文本的自定义按钮并使用按钮操作打开链接
    • 或者您甚至可以尝试在标签顶部放置一个自定义透明按钮。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      使用属性字符串:

      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。他还支持自定义链接和自定义颜色。

      【讨论】:

        猜你喜欢
        • 2011-02-11
        • 2016-07-25
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 2013-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多