本文介绍了在iphone中的文字下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在目标c iphone中加下划线?
是否有任何UILabel下划线文本的方法?
How to underline text in objective c iphone??Is there any method for underline text of UILabel??
推荐答案
子类 UILabel
并覆盖 drawRect
方法如下。下划线将相同的文字颜色
和文字对齐
作为标签:
Subclass UILabel
and override drawRect
method as below. Underline will have the same text color
and text alignment
as the label:
- (void)drawRect:(CGRect)rect
{
if([[self.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length])
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0); // RGBA
CGContextSetLineWidth(ctx, 1.0f);
CGSize tmpSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(200, 9999)];
// check text alignment
if(self.textAlignment == UITextAlignmentLeft) {
CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
CGContextAddLineToPoint(ctx, tmpSize.width, self.bounds.size.height - 1);
}else if(self.textAlignment == UITextAlignmentCenter) {
CGFloat startPoint = (self.frame.size.width - tmpSize.width) / 2;
CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
CGContextAddLineToPoint(ctx, tmpSize.width + startPoint, self.bounds.size.height - 1);
}else if (self.textAlignment == UITextAlignmentRight) {
CGFloat startPoint = (self.frame.size.width - tmpSize.width);
CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
CGContextAddLineToPoint(ctx, self.frame.size.width, self.bounds.size.height - 1);
}
CGContextStrokePath(ctx);
}
[super drawRect:rect];
}
这篇关于在iphone中的文字下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!