Для UILabel нет простого подчеркивания.Таким образом, есть два подхода:
- Использование
UIWebView
, где вы можете подчеркнуть нужный текст с помощью <span style="text-decoration:underline">underlined html text<span>
. - Custom
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];
}