1

如何在iphone中划线(目标c)???

4

4 回答 4

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];  
}
于 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 回答