zoukankan      html  css  js  c++  java
  • UILabel字体间距调整

    思路:

      写一个 UILbel的子类;在子类里面重新布置UILbel的字体间距;

    如代码 .h

    #import <UIKit/UIKit.h>
    
    @interface AdjustableUILable : UILabel
    {
        CGFloat characterSpacing;
    }
    
    @property CGFloat characterSpacing;
    @end
    

     代码 .m

    #import "AdjustableUILable.h"
    
    @implementation AdjustableUILable
    @synthesize characterSpacing;
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
    
        }
        return self;
    }
    
    
    - (void)drawTextInRect:(CGRect)rect
    {
        if (characterSpacing)
        {
            // Drawing code
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGFloat size = self.font.pointSize;
            
            CGContextSelectFont (context, [self.font.fontName UTF8String], size, kCGEncodingMacRoman);
            CGContextSetCharacterSpacing (context, characterSpacing);
            CGContextSetTextDrawingMode (context, kCGTextFill);
            
            // Rotate text to not be upside down
            CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
            CGContextSetTextMatrix(context, xform);
            const char *cStr = [self.text UTF8String];
            CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr));
        }
        else
        {
            // no character spacing provided so do normal drawing
            [super drawTextInRect:rect];
        }
    }
    
    @end
    

      如何使用:

        HistoryToday *yearDates = [HistoryToday today];
        AdjustableUILable *yearLabel = [[AdjustableUILable alloc]initWithFrame:CGRectMake(18, 6, 240, 30)];
        yearLabel.text = yearDates.year;
        yearLabel.characterSpacing = 14;
        [self.view addSubview:yearLabel];
    

      

  • 相关阅读:
    转载:山寨币凶猛
    Windows8.1 关机异常的解决
    Windows8、Windows8.1使用便签工具
    下载Sourceforge等国内无法下载站点文件的另一种方法
    专著出版成本计算
    PL2303 Windows8.1驱动
    转载:寒门再难出贵子
    华为荣耀品牌独立,子品牌战略能否实现新突破
    路由大战前夜,盘点智能路由的前世今生
    2020年实用工具推荐
  • 原文地址:https://www.cnblogs.com/cocoajin/p/3142078.html
Copyright © 2011-2022 走看看