2011-02-08 13 views

答えて

3

UILabelには単純な下線はありません。あなたは<span style="text-decoration:underline">underlined html text<span>に必要なテキストに下線を引くことができUIWebViewを使用

  1. :だから2つの方法があります。
  2. カスタムUILabel - これは短い一列のラベルの場合には良い考えです。あなたはUILabelを継承し、いくつかのCUILabelクラスを作成し、それを次のコードで@implementationセクションでdrawRect方法だ置き換える必要があります。別のオプションはCoreTextを使用することですNR4TRの答えに加えて

`

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

アルファ値をテキストの色から意図的に破棄していましたか? –

0

あなたが検出にWebViewの&チェックマーク "リンク" のチェックボックスを使用することができます& HTMLテキストに

何テキスト
htmlContentTxt = @"<a title="XYZ" href="http://www.XYZ.com">www.XYZ.com</a>"; 
    [webview loadHTMLString:htmlContentTxt baseURL:nil]; 
関連する問題