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 ...
随机推荐
- opencv setTo()
转载至 作者:跬步达千里 opencv的setTo函数是将图像设置为某个值; 例如: 1.有一个Mat src,想将他的值全部设置成0,则可以src.setTo(0) 2.setTo还有更为高级的用法 ...
- phpcms的一些问题 乱码,安装
一.乱码:我这的网站出现的乱码情况:后台栏目名乱码,迁站后更新缓存,再更新栏目,内容,前台都乱码. 找了半天原因,经过本地测试,没问题,一上线就出现问题,不同点就是线上的数据库版本是mysql5.5, ...
- c++ 面试题(数据库)
1,索引的原理: https://www.cnblogs.com/songwenjie/p/9414960.html https://blog.csdn.net/qq_32924343/article ...
- redis、mysql、mongdb的比较
特点: 1-1 MySQL:1. 使用c和c++编写,并使用了多种编译器进行测试,保证源代码的可移植性2. 支持多种操作系统3. 为多种编程语言提供可API4. 支持多线程,充分利用CPU资源优化的S ...
- mysql修改删除You can't specify target table for update in FROM clause的问题
表中出现重复数据,需要删除重复数据,只保留一条 DELETE FROM crm_participant WHERE id IN ( SELECT c.id cid FROM crm_participa ...
- C++ 获取字符串中的所有汉字
#include<iostream> using namespace std; int main() { char str[20] = "cd大家好df"; ...
- RFID数据清洗与数据清洗的区别
RFID数据清洗和一般数据清洗的不同: RFID数据清洗已经跨越到硬件范畴!造成脏数据的原因是硬件原理和硬件所处环境本身!要提高RFID数据清洗能力,就必须同时研究技术原理和环境本身之间的互动关系,而 ...
- CSS文本超出指定行数省略显示
单行: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 2行: font-size: 17px;overflow: hi ...
- eclipse 安装lombok插件
下载lombok 下载地址:https://projectlombok.org/downloads/lombok.jar 或者访问官网下载 https://projectlombok.org/ 安装 ...
- 微擎开发------day01
微擎的数据常量 $_GPC -- 全局请求变量 类型: array 说明: 合并请求参数, 包括 $_GET, $_POST, $_COOKIE的内容. 相同键名覆盖规则为 $_COOKIE 覆盖 ...