iOS开发笔记-一种任意字体、颜色混排UILabel的实现
最近开发新App,射妓狮给的图上出现一种不同大小字体混排的Label,就像下面这种:

想了想,最简单的方法是使用多个UILabel排列显示,但是这样不仅麻烦而且效果也不好,索性自定义UILabel来尽可能的满足使用灵活性。
实现方法
与正常自定义控件的方法类似,主要利用了CoreGraphics来动态绘制字体,但这里字体的参数都用NSArray存储,以尽最大可能不受具体内容约束,实现灵活性。
代码如下:
#import <UIKit/UIKit.h> @interface UnevenHeightLabel : UIView
@property (nonatomic) NSArray *strings;
@property (nonatomic) NSArray *fonts;
@property (nonatomic) NSArray *originY;
@property (nonatomic) NSArray *fontColors; -(instancetype) initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts originY:(NSArray *)originY stringColors:(NSArray *) colors;
@end
#import "UnevenHeightLabel.h"
#import "UIColor+HexColor.h" @implementation UnevenHeightLabel // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 1.0f);
CGRect startRect;
CGSize requiredSize;
float sumWidth=;
if(_strings!=nil&& _strings.count>){
for (int i=; i<_strings.count; i++) {
CGSize maxSize=rect.size;
requiredSize=[_strings[i] boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_fonts[i]} context:nil].size;
if(i==){
startRect=CGRectMake(, , maxSize.width, maxSize.height);
}
else{
startRect=CGRectMake(sumWidth, [_originY[i] floatValue], requiredSize.width, requiredSize.height); }
[_strings[i] drawInRect:startRect
withAttributes:@{NSFontAttributeName:_fonts[i],
NSForegroundColorAttributeName:_fontColors[i]}]; sumWidth=sumWidth+requiredSize.width; } }
} -(instancetype)initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts originY:(NSArray *)originY stringColors:(NSArray *)colors {
self=[super init];
self.strings=strings;
self.fonts=fonts;
self.originY=originY;
self.fontColors=colors;
//[self setNeedsDisplay];
return self;
} @end
Demo:
使用方法很简单,直接在需要使用的地方调用,如下:
UnevenHeightLabel *label=[[UnevenHeightLabel alloc] initWithUnevenHeightStrings:@[@"11.",@"",@"%"] stringFonts:@[[UIFont systemFontOfSize:],[UIFont systemFontOfSize:],[UIFont systemFontOfSize:]] originY:@[@,@,@] stringColors:@[[UIColor redColor],[UIColor blueColor],[UIColor greenColor]]];
label.frame=CGRectMake(, , , );
label.backgroundColor=[UIColor clearColor];
[self.view addSubview:label];
UnevenHeightLabel *mylabel=[[UnevenHeightLabel alloc] initWithUnevenHeightStrings:@[@"A",@"a",@"B",@"b",@"C",@"c"]
stringFonts:@[[UIFont systemFontOfSize:],
[UIFont systemFontOfSize:],
[UIFont systemFontOfSize:],
[UIFont systemFontOfSize:],
[UIFont systemFontOfSize:],
[UIFont systemFontOfSize:]]
originY:@[@,@,@,@,@,@]
stringColors:@[
[UIColor redColor],
[UIColor orangeColor],
[UIColor greenColor],
[UIColor blueColor],
[UIColor cyanColor],
[UIColor purpleColor]]];
[mylabel setFrame:CGRectMake(SCREEN_WIDTH/-, SCREEN_HEIGHT/-, , )];
[mylabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:mylabel];
效果如下:

iOS开发笔记-一种任意字体、颜色混排UILabel的实现的更多相关文章
- 【iOS学习笔记】改变状态栏字体颜色
Step1. info.plist中设置UIViewControllerBasedStatusBarAppearance为NO Step2. AppDelegate.m中添加 - (BOOL)appl ...
- iOS开发 - 第05篇 - 项目 - 12 - 图文混排
1.首页微博文字处理 对于之前微博项目中首页:微博文字中的用户名.话题.链接等文字须要高亮显示.表情字符串须要显示相应表情. 思路: 1>之前微博中的文字使用NSString,要达到不同文字的高 ...
- iOS开发笔记--使用blend改变图片颜色
最近对Core Animation和Core Graphics的内容东西比较感兴趣,自己之前也在这块相对薄弱,趁此机会也想补习一下这块的内容,所以之后几篇可能都会是对CA和CG学习的记录的文章. 在应 ...
- iOS开发笔记-两种单例模式的写法
iOS开发笔记-两种单例模式的写法 单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...
- 李洪强iOS开发之-修改状态栏的字体的颜色
李洪强iOS开发之-修改状态栏的字体的颜色 修改的效果: -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [ ...
- iOS开发笔记7:Text、UI交互细节、两个动画效果等
Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...
- iOS开发UITableViewCell的选中时的颜色设置(转)
iOS开发UITableViewCell的选中时的颜色设置 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...
- IOS开发-几种截屏方法
IOS开发-几种截屏方法 1. UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomSc ...
随机推荐
- MFC笔记5
1.MessageBox() 引用自(http://www.douban.com/note/40199603/) 一 函数原型及参数 function MessageBox(h ...
- ie7ajax 跨域 no transport 解决办法
客户端js <script src="jquery-1.8.0.min.js"></script> <script src="JavaScr ...
- Jenkin配置执行远程shell命令
转载自 http://www.cnblogs.com/parryyang/p/6261730.html 在利用jenkins进行集成化部署的时候,我们在部署生成的war包时,往往需要进行一些备份,或者 ...
- 简单了解pytorch的forward
import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.autogra ...
- 451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出
[抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...
- [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 查询Oracle 临时表空间使用情况[z]
[z]http://blog.itpub.net/28697282/viewspace-1441321/ SELECT d.tablespace_name “Name”, TO_CHAR(NVL(a. ...
- PHP开发——函数
函数的定义 l 函数是一段命名的代码段. 函数可以减轻工作量,减少重复的代码,方便后期维护. 函数的参数 l 实参:调用函数时,传递的参数就是实参,含有真正数据的. l 形参:定义函数时的参数. ...
- redis 一些操作命令
# 删除laravel keyredis-cli -h 10.9.103.15 -a password keys "laravel*" | xargs redis-cli -h 1 ...
- 一不注意,在Unity3D中DllImport 引起的Bug.
单要说这个Bug是很简单,但是得从头说起. 一些大型的网络游戏,或者加载比较多的一些场景时,如果要等待所有模型,贴图等各种资源文件加载完毕才能执行游戏,对用户将会是一个很头大的事情.所以就需要用到动态 ...