能添加图标的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' ...
随机推荐
- 【LOJ】#2082. 「JSOI2016」炸弹攻击 2
题解 想到n3发现思路有点卡住了 对于每个发射塔把激光塔和敌人按照极角排序,对于一个激光塔,和它转角不超过pi的激光塔中间夹的敌人总和就是答案 记录前缀和,用two-Points扫一下就行 代码 #i ...
- UEditor插入视频,Object Iframe等标签被过滤问题处理
UEditor插入视频由于兼容性问题,需要再处理一个视频代码,但是新版ueditor不支持Objec IFrame标签,所以要加入// xss过滤白名单 名单来源: https://raw.githu ...
- chrome使用技巧(转)
原文:http://www.cnblogs.com/liyunhua/p/4544738.html 阅读目录 写在前面 快速切换文件 在源代码中搜索 在源代码中快速跳转到指定的行 使用多个插入符进行选 ...
- shiro xml标准配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 公钥,密钥,HTTPS详解
公钥与私钥,HTTPS详解 1.公钥与私钥原理1)鲍勃有两把钥匙,一把是公钥,另一把是私钥2)鲍勃把公钥送给他的朋友们----帕蒂.道格.苏珊----每人一把.3)苏珊要给鲍勃写一封保密的信.她写完后 ...
- Android-Window(一)——初识Window
Android-Window(一)--初识Window 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/detail ...
- hiho1270 建造基地([Offer收割]编程练习赛1)
题目意思很简单, 一道类似于背包的问题,问恰好超过背包容量的最小取值.并且需要计算n次,每计算一次都要将所有的物品的价值w[i] /= t,最后输出背包n次的总使用容量. 如果无法实现输出”No An ...
- [POJ1655]Balancing Act
思路:DP求树的重心.对于每个结点$i$,$d_i=\displaystyle{\sum_{j\in s(i)}}d_j+1$.删去这个点能得到的最大子树大小即为$\max(\max\limits_{ ...
- 吴恩达-coursera-机器学习-week2
四.多变量线性回归(Linear Regression with Multiple Variables) 4.1 多维特征 4.2 多变量梯度下降 4.3 梯度下降法实践1-特征缩放 4.4 梯度下降 ...
- HDU 5745 La Vie en rose 暴力
La Vie en rose 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5745 Description Professor Zhang woul ...