最近开发新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的实现的更多相关文章

  1. 【iOS学习笔记】改变状态栏字体颜色

    Step1. info.plist中设置UIViewControllerBasedStatusBarAppearance为NO Step2. AppDelegate.m中添加 - (BOOL)appl ...

  2. iOS开发 - 第05篇 - 项目 - 12 - 图文混排

    1.首页微博文字处理 对于之前微博项目中首页:微博文字中的用户名.话题.链接等文字须要高亮显示.表情字符串须要显示相应表情. 思路: 1>之前微博中的文字使用NSString,要达到不同文字的高 ...

  3. iOS开发笔记--使用blend改变图片颜色

    最近对Core Animation和Core Graphics的内容东西比较感兴趣,自己之前也在这块相对薄弱,趁此机会也想补习一下这块的内容,所以之后几篇可能都会是对CA和CG学习的记录的文章. 在应 ...

  4. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法   单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...

  5. 李洪强iOS开发之-修改状态栏的字体的颜色

    李洪强iOS开发之-修改状态栏的字体的颜色 修改的效果: -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [ ...

  6. iOS开发笔记7:Text、UI交互细节、两个动画效果等

    Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...

  7. iOS开发UITableViewCell的选中时的颜色设置(转)

    iOS开发UITableViewCell的选中时的颜色设置   1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...

  8. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  9. IOS开发-几种截屏方法

    IOS开发-几种截屏方法 1.        UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomSc ...

随机推荐

  1. html5移动端查找

    用form包裹住input,修改input的类型为seach,然后给input绑定seach事件,当输入状态是输入键盘上会出现搜索,点击搜索就可以查找了 <form action="& ...

  2. TaskScheduler

    一初始化 在SparkContext初始化的时候,同时初始化三个对象.DAGScheduler,TaskScheduler,SchedulerBackend.DAGScheduler,前面已经讲到,做 ...

  3. URL重写中的中文参数问题

    在做搜索功能时,需要输入关键字,如果搜索出来的结果很多,又需要分页.这里用URL重写技术(即href="?keyword=关键字&page=分页数"),就涉及到了传递中文关 ...

  4. linux主机名设置

    有时会报错: 代理抛出异常错误: java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostExcept ...

  5. Python开发——函数【作用域】

    变量的查找是由内向外的 # 全局变量 name = 'yuan' # 定义函数 def change1(): print(name) # yuan # 调用函数 change1() print(nam ...

  6. boost的下载和安装(windows版)

    1 简介 boost是一个准C++标准库,相当于STL的延续和扩充,它的设计理念和STL比较接近,都是利用泛型让复用达到最大化. boost主要包含以下几个大类: 字符串及文本处理.容器.迭代器(it ...

  7. 点击导出table表格

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 18. pt-pmp

    pt-pmp 是一个非常简单的工具,可以用来获取MySQL的堆栈信息.工具首先获取运行过程中的mysqld堆栈信息,然后将相似的线程进行汇总排序,根据调用频繁程度从高到低打印出来. 查看pt-pmp的 ...

  9. C++ 提取网页内容系列之一

    标 题: C++ 提取网页内容系列作 者: itdef链 接: http://www.cnblogs.com/itdef/p/4171179.html 欢迎转帖 请保持文本完整并注明出处 首先分析网页 ...

  10. E: Sub-process /usr/bin/dpkg returned an error code

    E: Sub-process /usr/bin/dpkg returned an error code (1)错误解决 在用apt-get安装软件时出现了类似于install-info: No dir ...