zoukankan      html  css  js  c++  java
  • UILabel Text 加下划线

    .h文件
    #import <Foundation/Foundation.h>
    
    @interface CustomLabel : UILabel
    {
        BOOL _isEnabled;
    }
    
    @property (nonatomic ) BOOL isEnabled;
    
    @end
    
    .m文件
    #import "CustomLabel.h"
    
    @implementation CustomLabel
    
    @synthesize isEnabled = _isEnabled;
    
    - (void)drawTextInRect:(CGRect)rect{
        [super drawTextInRect:rect];
        
        CGSize textSize = [[self text] sizeWithFont:[self font]];
        CGFloat strikeWidth = textSize.width;
        CGRect lineRect;
        
        if ([self textAlignment] == NSTextAlignmentRight) {
            lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
        } else if ([self textAlignment] == NSTextAlignmentCenter) {
            lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
        } else {
            lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
        }
        
        if (_isEnabled) {
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextFillRect(context, lineRect);
        }
    }
    
    // 调用
    
    CustomLabel *_label;
     _label = [[CustomLabel alloc]initWithFrame:CGRectMake(50, 100, 0, 40)];
     _label.text = @"这是一个多么美好的世界啊";
     _label.backgroundColor = [UIColor clearColor];
     _label.isEnabled = YES;
     [_label sizeToFit];
      _label.textColor = [UIColor redColor];
     [self.view addSubview:_label];
  • 相关阅读:
    [codevs2800]送外卖
    python JSON处理
    python系统编码格式
    python,django,mysql版本号查询
    django开发总结:
    python之---类和实例
    django Q和F查询
    合并多个python list以及合并多个 django QuerySet 的方法
    python学习之---匿名函数,返回函数,偏函数
    python学习之---生成器
  • 原文地址:https://www.cnblogs.com/joesen/p/3200016.html
Copyright © 2011-2022 走看看