给UILabel 或者 UIButton标题加下划线
2016-05-15 19:34
423 查看
原文地址:http://blog.csdn.net/chaoyuan899/article/details/38306141
方法一:
[objc] view
plain copy
print?
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"查看所有中奖记录"];
NSRange strRange = {0,[str length]};
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_awardDisplayBtn setAttributedTitle:str forState:UIControlStateNormal];
方法二:
HyperlinksButton.h
[objc] view
plain copy
print?
#import <UIKit/UIKit.h>
@interface HyperlinksButton : UIButton
{
UIColor *lineColor;
}
-(void)setColor:(UIColor*)color;
@end
HyperlinksButton.m
[objc] view
plain copy
print?
#import "HyperlinksButton.h"
@implementation HyperlinksButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)setColor:(UIColor *)color{
lineColor = [color copy];
[self setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGFloat descender = self.titleLabel.font.descender;
if([lineColor isKindOfClass:[UIColor class]]){
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
直接将这个类 copy 到工程中,,然后将需要加下划线的 Button 类名改为 HyperlinksButton就可以了,提供了
setColor: 这个接口,可以设置下划线颜色,代码很简单,不解释了。UILabel 同理可得。
示例结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201911/16/dc4e4f185847a79bcf7feafd6c035408)
代码在这里。
方法一:
[objc] view
plain copy
print?
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"查看所有中奖记录"];
NSRange strRange = {0,[str length]};
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_awardDisplayBtn setAttributedTitle:str forState:UIControlStateNormal];
方法二:
HyperlinksButton.h
[objc] view
plain copy
print?
#import <UIKit/UIKit.h>
@interface HyperlinksButton : UIButton
{
UIColor *lineColor;
}
-(void)setColor:(UIColor*)color;
@end
HyperlinksButton.m
[objc] view
plain copy
print?
#import "HyperlinksButton.h"
@implementation HyperlinksButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)setColor:(UIColor *)color{
lineColor = [color copy];
[self setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGFloat descender = self.titleLabel.font.descender;
if([lineColor isKindOfClass:[UIColor class]]){
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
直接将这个类 copy 到工程中,,然后将需要加下划线的 Button 类名改为 HyperlinksButton就可以了,提供了
setColor: 这个接口,可以设置下划线颜色,代码很简单,不解释了。UILabel 同理可得。
示例结果:
代码在这里。
相关文章推荐
- 在spark中使用Hive报错error: not found: value sqlContext
- JSP内置对象--request对象
- iOS开发系列--UITableView全面解析
- 按钮添加活动指示器
- 关于hibernate中的错误:Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: tb_e
- poj 1141 Brackets Sequence
- POJ 1141 Brackets Sequence
- 国防科大校赛决赛-final(镜像赛) Problem A: XueXX and P-P String
- 国防科大校赛决赛-final(镜像赛) Problem C: XueXX and Chessboard
- 国防科大校赛决赛-final(镜像赛) Problem B: XueXX and Binary
- Burp Suite使用详解
- 使用 Equinox 开发 OSGi 应用程序
- poj 1458 Common Subsequence
- 【LeetCode】95. Unique Binary Search Trees II
- Java源码学习 -- java.lang.StringBuilder,java.lang.StringBuffer,java.lang.AbstractStringBuilder
- php中include()和require()的区别
- LeetCode 60. Permutation Sequence
- 在Android Studio中进行单元测试和UI
- 【GIT】使用GIT GUI 连接到conding码库教程
- OC NSNumber NSValue