能添加图标的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的更多相关文章

  1. WPF 带CheckBox、图标的TreeView

    WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提 ...

  2. Qt 怎样生成带图标的exe

    一.问题描述 当我们在 Windows 下用 VS 生成 exe 程序时,如果窗口程序指定了图标,那么生成的 exe 程序便是指定的图标模样. 但是,当使用 Qt Creator 编译程序却不同.即使 ...

  3. Android实现带图标的ListView

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/bear_huangzhen/article/details/23991119 Android实现带图 ...

  4. QT学习之如何在QToolBar中添加带图标的QToolButton并设置图标大小

    在网上查到了三种方法,找到一种比较好理解的. 使用QIcon类: QToolButton *toolBtn1 = new QToolButton(this); //创建QToolButton tool ...

  5. 右键菜单添加带图标的Notepad++

    给Notepad++ 加带图标右键菜单 方式一: 拷贝以下代码建立一个reg文件,替换相关路径,保存,双击运行加入注册表 Windows Registry Editor Version 5.00 [H ...

  6. 百度地图API多个点聚合时,标注添加的标签label地图刷新就丢失的问题解决

    当将自定义的Marker(含有Label)通过MarkerClusterer 管理的时候,当地图发生任何移动.缩放 的时候,Marker 的Label 就会自动消失. 这个问题主要是由于百度的点聚合A ...

  7. graph使泳道图的label横向显示

    1.如果需要将label靠左边对齐,则必须重写底层源码 新增mxText的一个构造器,主要是增加了一个参数:x(代表当前的cell) function mxText(a, b, c, d, e, f, ...

  8. echarts 饼状图调节 label和labelLine的位置

    原理 使用一个默认颜色为透明的,并且只显示labelLine的饼状图 然后通过调节这个透明的饼状图 以达到修改labelLine的位置 echarts地址 https://gallery.echart ...

  9. SAP(ABAP) 显示等待图标的FM:SAPGUI_PROGRESS_INDICATOR-SAP进度条

    在执行一些数据量大的报表时候,为了防止用户认为是死机,可以再程序中添加正在处理的图标,可以CALL一个 FM来实现. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' ...

随机推荐

  1. 【hadoop】har://

    来源:http://hadoop.apache.org/docs/r1.0.4/cn/hadoop_archives.html 在hadoop代码中发现了一个很奇怪的路径,由har://开头.经查询, ...

  2. 上传文件异常问题 | 413 Request Entity Too Large

    开发中遇到这样的问题:一个上传文件的功能,内网测试都正常了,但是发布到外网就无法上传大点的(大于1MB)文件,更奇怪的是,后台还没有任何的异常信息. 于是就用Http抓包工具(HttpDetect)看 ...

  3. AxureRP8实战手册

    基础操作篇 本篇包含56种常见的基础操作,初学者应在掌握本篇内容后再进行实战案例篇的学习,以免产生学习障碍.同时,建议具备一定基础的读者学习本篇中相对生疏的内容,并加以掌握. 第1章 使用元件 本文目 ...

  4. Windows下SVN服务器搭建方法整理(apache)

    http://skydream.iteye.com/blog/437959 http://www.cnblogs.com/liuke209/archive/2009/09/23/1572858.htm ...

  5. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  6. zTree搜索

    自写 zTree搜索功能 -- 关键字查询 -- 递归无限层 唠叨一哈 前两天朋友跟我说要一个ztree的搜索功能,我劈头就是一巴掌:这种方法难道无数前辈还做少了?自己去找,我很忙~然后我默默地蹲着写 ...

  7. 010 innerHtml的使用

    1.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  8. 【Android Studio】为Android Studio设置HTTP代理

    [Android Studio]为Android Studio设置HTTP代理   大陆的墙很厚很高,初次安装Android Studio下载SDK等必定失败,设置代理方法如下: 1. 到androi ...

  9. MATLAB检查指定路径中的子文件夹中的文件名中是否带有空格

    测试文件夹为: clear;close all;clc; %% %程序实现的功能 %检查指定路径中的子文件夹中的文件名中是否带有空格,并去掉文件名中的空格 %% %程序中用到的之前不清楚的函数如下 % ...

  10. Spring拦截器和过滤器

    什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...