能添加图标的label
能添加图标的label

效果

源码
https://github.com/YouXianMing/UI-Component-Collection 中的 IconEdgeInsetsLabel
//
// IconEdgeInsetsLabel.h
// EdgeInsetLabel
//
// Created by YouXianMing on 16/6/22.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> typedef enum : NSUInteger { kIconAtLeft,
kIconAtRight, } EIconEdgeDirection; @interface IconEdgeInsetsLabel : UILabel @property (nonatomic, strong) UIView *iconView;
@property (nonatomic) UIEdgeInsets edgeInsets;
@property (nonatomic) EIconEdgeDirection direction;
@property (nonatomic) CGFloat gap; - (void)sizeToFitWithText:(NSString *)text; @end
//
// IconEdgeInsetsLabel.m
// EdgeInsetLabel
//
// Created by YouXianMing on 16/6/22.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "IconEdgeInsetsLabel.h"
#import "UIView+SetRect.h" @interface IconEdgeInsetsLabel () @property (nonatomic, weak) UIView *oldIconView; @end @implementation IconEdgeInsetsLabel - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { UIEdgeInsets insets = self.edgeInsets; CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets) limitedToNumberOfLines:numberOfLines]; rect.origin.x -= insets.left;
rect.origin.y -= insets.top;
rect.size.height += (insets.top + insets.bottom);
_iconView && [_iconView isKindOfClass:[UIView class]] ?
(rect.size.width += (insets.left + insets.right + _gap + _iconView.frame.size.width)) :
(rect.size.width += (insets.left + insets.right)); return rect;
} - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = self.edgeInsets; if (self.iconView) { if (self.direction == kIconAtLeft) { _iconView.left = insets.left;
_iconView.centerY = self.middleY;
insets = UIEdgeInsetsMake(insets.top, insets.left + _gap + _iconView.frame.size.width, insets.bottom, insets.right); } else if (self.direction == kIconAtRight) { _iconView.right = self.width - insets.right;
_iconView.centerY = self.middleY;
insets = UIEdgeInsetsMake(insets.top, insets.left, insets.bottom, insets.right + _gap + _iconView.frame.size.width);
}
} [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
} - (void)sizeToFitWithText:(NSString *)text { self.text = text;
[self sizeToFit];
} #pragma mark - setter & getter. @synthesize iconView = _iconView; - (void)setIconView:(UIView *)iconView { _oldIconView && [_oldIconView isKindOfClass:[UIView class]] ? ([_oldIconView removeFromSuperview]) : ; _iconView = iconView;
_oldIconView = iconView;
iconView.x = .f;
iconView.y = .f; [self addSubview:iconView];
} - (UIView *)iconView { return _iconView;
} @end
细节
1. 继承自UILabel

2. 重载了UILabel的两个方法

能添加图标的label的更多相关文章
- WPF 带CheckBox、图标的TreeView
WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提 ...
- Qt 怎样生成带图标的exe
一.问题描述 当我们在 Windows 下用 VS 生成 exe 程序时,如果窗口程序指定了图标,那么生成的 exe 程序便是指定的图标模样. 但是,当使用 Qt Creator 编译程序却不同.即使 ...
- Android实现带图标的ListView
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/bear_huangzhen/article/details/23991119 Android实现带图 ...
- QT学习之如何在QToolBar中添加带图标的QToolButton并设置图标大小
在网上查到了三种方法,找到一种比较好理解的. 使用QIcon类: QToolButton *toolBtn1 = new QToolButton(this); //创建QToolButton tool ...
- 右键菜单添加带图标的Notepad++
给Notepad++ 加带图标右键菜单 方式一: 拷贝以下代码建立一个reg文件,替换相关路径,保存,双击运行加入注册表 Windows Registry Editor Version 5.00 [H ...
- 百度地图API多个点聚合时,标注添加的标签label地图刷新就丢失的问题解决
当将自定义的Marker(含有Label)通过MarkerClusterer 管理的时候,当地图发生任何移动.缩放 的时候,Marker 的Label 就会自动消失. 这个问题主要是由于百度的点聚合A ...
- graph使泳道图的label横向显示
1.如果需要将label靠左边对齐,则必须重写底层源码 新增mxText的一个构造器,主要是增加了一个参数:x(代表当前的cell) function mxText(a, b, c, d, e, f, ...
- echarts 饼状图调节 label和labelLine的位置
原理 使用一个默认颜色为透明的,并且只显示labelLine的饼状图 然后通过调节这个透明的饼状图 以达到修改labelLine的位置 echarts地址 https://gallery.echart ...
- SAP(ABAP) 显示等待图标的FM:SAPGUI_PROGRESS_INDICATOR-SAP进度条
在执行一些数据量大的报表时候,为了防止用户认为是死机,可以再程序中添加正在处理的图标,可以CALL一个 FM来实现. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' ...
随机推荐
- pyqt5-基础
PyQt5是一套来自Digia的Qt5应用框架和Python的粘合剂.支持Python2.x和Python3.x版本. PyQt5以一套Python模块的形式来实现功能.它包含了超过620个类,600 ...
- NodeJS学习:环境变量
简介 环境变量(environment variables) 不属于 NodeJS 范畴,它是操作系统用于设定执行环境的参数.会在程序运行时传递给应用程序. NodeJS 获取环境变量,是通过 glo ...
- ADO.Net1
一.ADO.Net 数据库连接技术 二.查询 1.步骤: 1)使用数据库空间:using System.Data.SqlClient; 2)连接数据库 3)创建数据库操作命令 4)输入操作命令 5)开 ...
- prometheus如何使用blackbox-exporter来获取k8s的网络性能
如果学会了如何抓取app的metrics,则blackbox-exporter抓取网络性能套路是相同的. 一,在blackbox-exporter的blackbox.yml里配置好抓取模块,常见的是h ...
- AndroidStudio3.0以上版本的坑
原文:https://blog.csdn.net/ytfunnysite/article/details/78864556 1.Error:Failed to resolve: com.android ...
- JS几种变量交换
JS几种变量交换方式以及性能分析对比 原文 原文是自己博客上发布的JS几种变量交换方式以及性能分析对比 前言 “两个变量之间的值得交换”,这是一个经典的话题,现在也有了很多的成熟解决方案,本文主要 ...
- 黑马程序员_java基础笔记(14)...交通灯管理系统_编码思路及代码
—————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 1,面试题——交通灯管理系统 模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: ...
- 多线程学习笔记四之Condition实现分析
目录 简介 等待队列 nextWaiter 源码分析 await() signal() signalAll() 总结 简介 在使用内置锁synchronized时,通过调用java.lang.Ob ...
- 某题目2 状压DP
Description 对于一个数列,其混乱度定义为连续相等的数的段数.如:1 2 1 2 1,其混乱度为5,而:1 2 2 3 3,其混乱度为3.现给出一个数列,允许取出k个数并允许插入数列中的任意 ...
- BZOJ 3101: N皇后 构造
3101: N皇后 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3101 Description n*n的棋盘,在上面摆下n个皇后,使其 ...