iOS开发-UILabel和UIButton添加下划线
2015-06-17 07:27
585 查看
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择第一种,第二种有兴趣的可以自己研究一下。
UILabel设置下划线:
UIButton设置下划线:
最终效果如下:
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 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"博文地址:http://www.cnblogs.com/xiaofeixiang 博客园-FlyElephant QQ群:228407086"]]; NSRange contentRange = {0,[content length]}; [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange]; label.attributedText = content; [self.view addSubview:label];
UIButton设置下划线:
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 80, 30)]; NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"FlyElephant"]; NSRange titleRange = {0,[title length]}; [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange]; [button setAttributedTitle:title forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor redColor]]; [button.titleLabel setFont:[UIFont systemFontOfSize:14]]; [self.view addSubview:button];
最终效果如下:
相关文章推荐
- listener does not currently know of service requested in connect descriptor
- OpenProcessToken,LookupPrivilegeValue,AdjustTokenPrivileges提升进程权限
- MBaaS – IBM Mobile Cloud Services, Bluemix & MobileFirst
- Xcode6中segue取消原push与modal(deprecated)
- stroryboard利用Segue实现界面传值
- UILocalNotification 本地通知的应用实例
- 设计初学者如何快速进步?
- JSP deleteCategory.jsp (line: 9, column: 67) The JSP specification requires that an attribute name i
- UIButton的一些基本用法
- HBuild+Mui学习笔记(一):真机运行提示错误总结
- ligerUI webUI 传参
- Unique Paths II
- 杭电ACM1216——Assistance Required~~筛选法的应用
- Request URI = context path + servlet path + path info
- IOS Exception2 this class is not key value coding-compliant for the key Click
- android源码之Builder
- poj 3061 Subsequence 【尺取法 or STL lower_bound】
- unique_ptr优于auto_ptr的原因
- CocoaUI 是一个强大的 iOS UI 框架
- sublime text3 中 CTags: Rebuild Tags执行之乱码解决方法