IOS UIlabel 、UIButton添加下划线
1.给UILabel 添加下划线
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.backgroundColor = [UIColor redColor];
label.numberOfLines = ;
NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"您的待办业务条fasdfasdfosadfjasdkljfklasjklfjasdlkjfklasdjklfjalskdjflkadsj"]];
NSRange contentRange = {,[content length]};
[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
label.attributedText = content;
[self.view addSubview:label];
2. http://blog.csdn.net/chaoyuan899/article/details/38306141
方法一:
- 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
- #import <UIKit/UIKit.h>
- @interface HyperlinksButton : UIButton
- {
- UIColor *lineColor;
- }
- -(void)setColor:(UIColor*)color;
- @end
HyperlinksButton.m
- #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 同理可得。
示例结果:
代码在这里。
IOS UIlabel 、UIButton添加下划线的更多相关文章
- iOS开发-UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...
- UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...
- iOS 给UILabel文字加下划线
摘自:http://blog.sina.com.cn/s/blog_6cd380c10101b6hn.html //带下划线的“注” NSMutableAttributedString可变的属性字符串 ...
- vim显示行号、语法高亮、自动缩进、添加下划线的设置
ubuntu默认是没有安装vim的,所以设置以前请先安装vim:sudo apt-get install vim. 然后 打开vim的配置文件:sudo vim /etc/vim/vimrc 或者 s ...
- Android 给EditText添加下划线
在安卓高版本,默认是有下划线的,其默认下划线的颜色是由其主题颜色来控制的! 控制如下: <style name="AppTheme" parent="Theme.A ...
- "为文本添加下划线"组件:<u> —— 快应用组件库H-UI
 <import name="u" src="../Common/ui/h-ui/text/c_tag_underline"></impor ...
- UILabel,文字添加下划线,中划线
//显示下划线 //中划线 // NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber nu ...
- TextView 为部分文字添加下划线,并实现单击事件
在开发应用的过程中经常会遇到显示一些不同的字体风格的信息,如关键词高亮显示的等.对于类似的情况,一般我们会想着使用多个TextView去实现,对于每个TextView设置不同的字体风格来满足需求. ...
- 【界面优化】使用viewpagerindicator添加下划线滑动动画
开源代码viewpagerindicator里面没有实现tab下划线切换过程中的移动动画,都是很突兀的多个fragement之间的切换,导致用户体验略差,google了下相关问题,发现一片博文: ht ...
随机推荐
- flash+xml无法显示中文的解决办法
flash+xml用来做图片动态浏览效果相当不错,被广泛运用于电子相册制作,很多朋友都会从网上下载一些相关的flash源码下载参考,但是经常发现在使用过程中,修改了xml文件中的英文后要么文本不显示, ...
- metasploit study
load db_trackerdb_nmap -T Aggressive -sV -n -O -v 192.168.0.107 绑定shelluse exploit/windows/smb/ms08_ ...
- 搭建k8s集群的手顺
https://www.cnblogs.com/netsa/category/1137187.html
- Hibernate之load和get的差别
load和get都会能够起到从数据库中获取持久态数据的作用.可是还有些略微的差别的. 參考以下的这个样例: @Test(expected = IllegalArgumentException.clas ...
- Octave入门基础
Octave入门基础 一.简单介绍 1.1 Octave是什么? Octave是一款用于数值计算和画图的开源软件.和Matlab一样,Octave 尤其精于矩阵运算:求解联立方程组.计算矩阵特征值和特 ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- Android开发之WebView具体解释
概述: 一个显示网页的视图.这个类是你能够滚动自己的Web浏览器或在你的Activity中简单地显示一些在线内容的基础.它使用了WebKit渲染引擎来显示网页,包含向前和向后导航的方法(通过历史记录) ...
- nginx源代码分析--监听套接字的创建 套接字的监听 HTTP请求创建连接
作为一个webserver,那么肯定是有监听套接字的,这个监听套接字是用于接收HTTP请求的,这个监听套接字的创建是依据配置文件的内容来创建的,在nginx.conf文件里有多少个地址就须要创建多少个 ...
- SQLServer理解copyonly备份操作
Always在添加数据库的过程中如果同步首选项选择的是“完整”,那么就会在主副本上执行copyonly的完整备份和日志备份在辅助副本上执行还原操作,也正是这个操作让我对copyonly有了新的理解.接 ...
- Android微信分享功能实例+demo
Android微信分享功能实例 1 微信开放平台注册 2 获得appId,添加到程序中,并运行程序 3 使用应用签名apk生成签名,添加到微信开放平台应用签名,完成注册 4 测试分享功能. 有问题请留 ...