UILabel设置下划线
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 310, 50)];
label.backgroundColor = [UIColor redColor];
[label setLineBreakMode:NSLineBreakByWordWrapping];
label.numberOfLines =3;
[label setFont:[UIFont systemFontOfSize:14]];
NSMutableAttributedString*content = [[NSMutableAttributedStringalloc]initWithString:[NSStringstringWithFormat:@"博文地址:http://www.cnblogs.com/xiaofeixiang博客园-FlyElephant QQ群:228407086"]];
NSRangecontentRange = {0,[content length]};
[content addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle] range:contentRange];
label.attributedText = content;
[self.view addSubview:label];
UIButton设置下划线:
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 80, 30)];
NSMutableAttributedString*title = [[NSMutableAttributedStringalloc] initWithString:@"FlyElephant"];
NSRangetitleRange = {0,[title length]};
[title addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle] range:titleRange];
[button setAttributedTitle:title
forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button.titleLabel setFont:[UIFont systemFontOfSize:14]];
[self.view addSubview:button];
方法二:
HyperlinksButton.h
[objc]view plaincopyprint?
#import
@interfaceHyperlinksButton : UIButton
{
UIColor*lineColor;
}
-(void)setColor:(UIColor*)color;
@end
HyperlinksButton.m
[objc]view plaincopyprint?
#import "HyperlinksButton.h"
@implementationHyperlinksButton
- (id)initWithFrame:(CGRect)frame
{
self= [superinitWithFrame:frame];
if(self) {
}
returnself;
}
-(void)setColor:(UIColor*)color{
lineColor = [colorcopy];
[selfsetNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGRect textRect =self.titleLabel.frame;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGFloat descender =self.titleLabel.font.descender;
if([lineColorisKindOfClass:[UIColorclass]]){
CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor);
}
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y+ textRect.size.height+ descender+1);
CGContextAddLineToPoint(contextRef, textRect.origin.x+ textRect.size.width, textRect.origin.y+ textRect.size.height+ descender+1);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
@end