1. .h文件
  2. #import <Foundation/Foundation.h>
  3.  
  4. @interface CustomLabel : UILabel
  5. {
  6. BOOL _isEnabled;
  7. }
  8.  
  9. @property (nonatomic ) BOOL isEnabled;
  10.  
  11. @end
  12.  
  13. .m文件
  14. #import "CustomLabel.h"
  15.  
  16. @implementation CustomLabel
  17.  
  18. @synthesize isEnabled = _isEnabled;
  19.  
  20. - (void)drawTextInRect:(CGRect)rect{
  21. [super drawTextInRect:rect];
  22.  
  23. CGSize textSize = [[self text] sizeWithFont:[self font]];
  24. CGFloat strikeWidth = textSize.width;
  25. CGRect lineRect;
  26.  
  27. if ([self textAlignment] == NSTextAlignmentRight) {
  28. lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/, strikeWidth, );
  29. } else if ([self textAlignment] == NSTextAlignmentCenter) {
  30. lineRect = CGRectMake(rect.size.width/ - strikeWidth/, rect.size.height/, strikeWidth, );
  31. } else {
  32. lineRect = CGRectMake(, rect.size.height/, strikeWidth, );
  33. }
  34.  
  35. if (_isEnabled) {
  36. CGContextRef context = UIGraphicsGetCurrentContext();
  37. CGContextFillRect(context, lineRect);
  38. }
  39. }
  40.  
  41. // 调用
  42.  
  43. CustomLabel *_label;
  44. _label = [[CustomLabel alloc]initWithFrame:CGRectMake(, , , )];
  45. _label.text = @"这是一个多么美好的世界啊";
  46. _label.backgroundColor = [UIColor clearColor];
  47. _label.isEnabled = YES;
  48. [_label sizeToFit];
  49. _label.textColor = [UIColor redColor];
  50. [self.view addSubview:_label];

UILabel Text 加下划线的更多相关文章

  1. iOS 给UILabel文字加下划线

    摘自:http://blog.sina.com.cn/s/blog_6cd380c10101b6hn.html //带下划线的“注” NSMutableAttributedString可变的属性字符串 ...

  2. iOS初学,关于变量加下划线问题

    为什么做ios开发,变量前要加下划线才有用? 看到这个哥们的解释后,终于明白了,转帖到此. 链接在此:http://www.cocoachina.com/bbs/read.php?tid=234290 ...

  3. Markdown - 如何给文本加下划线

    解决方法 Markdown可以和HTML的语法兼容,可以通过HTML的标签来实现效果: 写法 效果 <u>下划线</u> 下划线 这里解释下,u指的是underline下划线. ...

  4. css给文字加下划线

    语法:linear-gradient(direction, color-stop 1, color-stop 2,……) 简单用法:background-image: linear-gradient( ...

  5. android假设给TextView或EditText的email链接加下划线,并在点击在email连接上能够弹框显示

    怎样把textview的一些文字加上背景色: Spannable str = new SpannableString("#fdsfdfsdfdsfd#"); Matcher mat ...

  6. android里TextView加下划线的几种方式

    如果是在资源文件里: <resources> <string name="hello"><u>phone:0123456</u>&l ...

  7. ios下划线变量:为什么变量前要加下划线才有用?

    先看一段代码.   复制代码 appdelegate.h @property (weak) IBOutlet NSMatrix *StockType; @property (weak) IBOutle ...

  8. Android TextView加下划线的几种方式

    如果是在资源文件里: <resources> <</u></string> <string name="app_name">M ...

  9. UILabel 字体下方加下划线

        UILabel *knowKankan = [[UILabel alloc] initWithFrame:CGRectMake(0, MAINHEIGHT - 78 , MAINWIDTH, ...

随机推荐

  1. textChanged(*)重点

    # -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...

  2. UITableView使用总结和性能优化

    UITableView使用总结和性能优化    UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.如 果我们查看UITabl ...

  3. c++之 变量

    变量的基本操作 变量就是一个可以变化的量,变量由变量类型.变量名.初始值(可选)组成,例如: int abc = 10; 变量类型:int 变量名:abc 初始值:10 // 该值为可选项,在创建变量 ...

  4. Ubuntu14.04配置cuda-convnet

    转载请注明:http://blog.csdn.net/stdcoutzyx/article/details/39722999 在上一个链接中,我配置了cuda,有强大的GPU,自然不能暴殄天物,让资源 ...

  5. 跟我学系列教程——《13天让你学会Redis》火热报名中

    学习目标 每天2小时,13天让你学会Redis. 本课程针对Redis新手,甚至连Redis是什么都没有听说过的同学.课程会具体介绍Redis是什么以及为什么要使用Redis,结合项目实践旨在让学生从 ...

  6. POJ 1469 ZOJ1140 二分匹配裸题

    很裸,左点阵n,右点阵m 问最大匹配是否为n #include <cstdio> #include <cstring> #include <vector> usin ...

  7. [Hapi.js] Extending the request with lifecycle events

    Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...

  8. WEB服务器4--IIS中网站、Web应用程序和虚拟目录

    网站.Web应用程序和虚拟目录 在IIS中可以创建网站.Web 应用程序和虚拟目录,以便与计算机网络上的用户共享信息. “网站”.“Web 应用程序”和“虚拟目录”这三个概念的关系如图 8‑1所示. ...

  9. jQuery 事件 - error() 方法

    实例 如果图像不存在,则用一段预定义的文本取代它: $("img").error(function(){ $("img").replaceWith(" ...

  10. python的编码问题研究------使用scrapy体验

    python转码译码 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...