能添加图标的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' ...
随机推荐
- wpf 千位符 格式化字符串
StringFormat={}{0:N2}}//格式话字符串,增加千位符,2保留小数点后2位 StringFormat={}{0:N0}}//格式话字符串,增加千位符,无小数点后
- unit测试出现异常:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util
在进行单元测试时,测试出现异常 Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform ...
- win10编译caffe跑faster-rcnn(cuda7.5)
2017年1月13日 15:46:04 github.com/Microsoft/caffe这版现在不算是BVLC/caffe的官方windows分支:官方windows分支是一个叫willyd的家伙 ...
- 【LOJ】#2181. 「SDOI2015」排序
题解 还以为是啥毒瘤题 然后是个搜索题 复杂度算起来挺大 然后跑起来就连0.1ms不到= = 就是从大到小进行每种操作,搜出来一种操作就乘上一个操作数的阶乘就行 如果现在进行的操作操作\(2^i\)那 ...
- VIM中使用S查找并替换
vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法(vi 真是强大啊,还有很多需要学习),记录几种在此,方便以后查询.:s/vivian/sk ...
- 使用ajax与jqplot的小体会
在使用ajax与jqplot时遇到了传值的问题!一开始都不知值是怎么传过去的,只找到了例子是以<div id="data">原始数据</div>这样子来接收 ...
- Go面试题精编100题
Golang精编100题 选择题 1. [初级]下面属于关键字的是()A. funcB. defC. structD. class 参考答案:AC 2. [初级]定义一个包内全局字符串变量,下 ...
- Vakuum开发笔记02 核心与安全问题
3.judger核心设计 评测系统最重要部分就是评测核心了(judger).核心judger负责了编译.执行.检查三大部分,也就是评测系统的灵魂所在,因此judger设计的好坏,直接影响到整个评测系统 ...
- JDK 动态代理的简单理解
动态代理 代理模式是 Java 中的常用设计模式,代理类通过调用被代理类的相关方法,提供预处理.过滤.事后处理等服务,动态代理及通过反射机制动态实现代理机制.JDK 中的 java.lang.refl ...
- 微信小程序自定义组件封装及父子间组件传值
首先在我们可以直接写到需要的 page 中,然后再进行抽取组件,自定义组件建议 wxzx-xxx 命名 官网地址:https://developers.weixin.qq.com/miniprogra ...