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 ...
随机推荐
- 云笔记项目-笔记列表弹出"分享移动删除"子菜单
业务需求: 笔记列表里还有一个按钮可以弹出子菜单,要求做到以下几点: (1)点击选中的笔记行的弹出按钮后,弹出子菜单,再次点击,子菜单收回. (2)选中其他笔记后,子菜单消失.效果如下图所示: 业务分 ...
- Selenium Webdriver元素定位的八种常用方式(转载)
转载自 https://www.cnblogs.com/qingchunjun/p/4208159.html 在使用selenium webdriver进行元素定位时,通常使用findElement或 ...
- linux增加,删除用户组,解压缩命令,VIM使用命令
ln -s 建立软连接 ln -s /usr/bin/fdfs_trackerd /usr/local/bin ln -s /usr/bin/stop.sh /usr/ ...
- 199. Binary Tree Right Side View 从右侧看的节点数
[抄题]: Given a binary tree, imagine yourself standing on the right side of it, return the values of t ...
- 前端移动开发之rem
前言 作为一名前端工程师,我们不仅要会PC端开发,还要会移动端开发,而且现在移动端占据主要流量,所以掌握移动端开发的技能更是必须的. 那么进行移动端的开发,什么是必须,我们想要的效果是什么? 自适应. ...
- Linux-echo、cat命令详解(14)
echo:显示一段文字 比如: echo hello,串口上就显示hello echo hello > /dev/tty1, LCD上便显示hello字段 cat:查看一个文件的内容 比如: c ...
- 网络编程初识和socket套接字
网络的产生 不同机器上的程序要通信,才产生了网络:凡是涉及到倆个程序之间通讯的都需要用到网络 软件开发架构 软件开发架构的类型:应用类.web类 应用类:qq.微信.网盘.优酷这一类是属于需要安装的桌 ...
- 获取当前最顶层的ViewController
- (UIViewController *)topViewController { UIViewController *resultVC; resultVC = [self _topViewContr ...
- NCUAP 利用java自带方法实现导入excel取数据
final JComponent parent = getModel().getContext().getEntranceUI(); JFileChooser chooser = new JFileC ...
- ssms创建链接服务器