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]; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具