1

iphone(objective c)でテキストに下線を引く方法???

4

4 に答える 4

3

UILabel には単純な下線はありません。したがって、2 つのアプローチがあります。

  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];  
}
于 2011-02-08T11:17:38.740 に答える
1

NR4TR の回答に加えて、別のオプションは CoreText を使用することです。

于 2012-01-06T02:21:26.903 に答える
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
于 2012-07-12T15:26:11.340 に答える
0

検出でwebviewとチェックマークの「リンク」チェックボックスを使用して、htmlテキストを指定できます

    htmlContentTxt = @"<a title="XYZ" href="http://www.XYZ.com">www.XYZ.com</a>";
    [webview loadHTMLString:htmlContentTxt baseURL:nil]; 
于 2013-01-01T08:43:14.050 に答える