2011-02-08 50 views

回答

3

UILabel沒有簡單的下劃線。因此,有兩種方法:

  1. 使用UIWebView在那裏你可以強調你需要<span style="text-decoration:underline">underlined html text<span>文本。
  2. 自定義UILabel - 對於短的單線標籤,這是一個好主意。您應該創建一些CUILabel類繼承UILabel和替換它的drawRect方法@implementation部分用下面的代碼:

`

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(ctx, 0.0f/255.0f, 0.0f/255.0f, 255.0f/255.0f, 1.0f); // Your underline color 
    CGContextSetLineWidth(ctx, 1.0f); 

    UIFont *font = [UIFont systemFontOfSize:16.0f]; 
    CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT); 
    CGSize labelSize; 
    labelSize = [self.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

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

    CGContextStrokePath(ctx); 

    [super drawRect:rect]; 
} 
1

除了NR4TR的回答另一種選擇是使用CoreText。

0

我改變NR4TR的代碼更自動:

#import "UILabelUnderlined.h" 

@implementation UILabelUnderlined 

@synthesize underlineWidth; 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    self.underlineWidth = 1.0f; 
} 
return self; 
} 

- (void)drawRect:(CGRect)rect { 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 

CGFloat red; 
CGFloat green; 
CGFloat blue; 
CGFloat alpha; 
[self.textColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

CGContextSetRGBStrokeColor(ctx, red, green, blue, 1.0f); // Your underline color 
CGContextSetLineWidth(ctx, self.underlineWidth); 

CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT); 
CGSize labelSize; 
labelSize = [self.text sizeWithFont:self.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

CGContextMoveToPoint(ctx, 10, self.bounds.size.height - 1); 
CGContextAddLineToPoint(ctx, labelSize.width + 10, self.bounds.size.height - 1); 

CGContextStrokePath(ctx); 

[super drawRect:rect]; 
} 

@end 
+0

是否從文本顏色故意丟棄了alpha值? – 2012-10-02 11:55:21

0

可以使用的WebView &對勾 「鏈接」 複選框,在檢測&給HTML文本

htmlContentTxt = @"<a title="XYZ" href="http://www.XYZ.com">www.XYZ.com</a>"; 
    [webview loadHTMLString:htmlContentTxt baseURL:nil]; 
相關問題