[控件] LabelView
LabelView


此LabelView是用来将Label显示在固定的View上的,需要计算Label的高度与宽度.
源码:
NSString+StringHeight.h 与 NSString+StringHeight.m
//
// NSString+StringHeight.h
// USA
//
// Created by YouXianMing on 14/12/10.
// Copyright (c) 2014年 fuhuaqi. All rights reserved.
// #import <Foundation/Foundation.h> @interface NSString (StringHeight) /**
* 计算文本的高度
*
* @param font 字体
* @param width 固定的宽度
*
* @return 高度
*/
- (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width; /**
* 计算文本的宽度
*
* @param font 字体
*
* @return 宽度
*/
- (CGFloat)widthWithLabelFont:(UIFont *)font; @end
//
// NSString+StringHeight.m
// USA
//
// Created by YouXianMing on 14/12/10.
// Copyright (c) 2014年 fuhuaqi. All rights reserved.
// #import "NSString+StringHeight.h" @implementation NSString (StringHeight) - (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width {
CGFloat height = ; if (self.length == ) {
height = ;
} else { // 字体
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:.f]};
if (font) {
attribute = @{NSFontAttributeName: font};
} // 尺寸
CGSize retSize = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size; height = retSize.height;
} return height;
} - (CGFloat)widthWithLabelFont:(UIFont *)font {
CGFloat retHeight = ; if (self.length == ) {
retHeight = ;
} else { // 字体
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:.f]};
if (font) {
attribute = @{NSFontAttributeName: font};
} // 尺寸
CGSize retSize = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, )
options:
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size; retHeight = retSize.width;
} return retHeight;
} @end
LabelView.h 与 LabelView.m
//
// LabelView.h
// YXMWeather
//
// Created by XianMingYou on 15/2/16.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h>
#import "NSString+StringHeight.h" @interface LabelView : UIView /**
* 文本
*/
@property (nonatomic, strong) NSString *text; /**
* 文本颜色
*/
@property (nonatomic, strong) UIColor *textColor; /**
* 文本字体
*/
@property (nonatomic, strong) UIFont *font; /**
* 背景色
*/
@property (nonatomic, strong) UIColor *color; /**
* 距离顶部的距离
*/
@property (nonatomic) CGFloat gapFromTop; /**
* 距离底部的距离
*/
@property (nonatomic) CGFloat gapFromBottom; /**
* 距离左侧的距离
*/
@property (nonatomic) CGFloat gapFromLeft; /**
* 距离右侧的距离
*/
@property (nonatomic) CGFloat gapFromRight; /**
* 创建出view
*/
- (void)buildView; /**
* 创建出默认配置的label
*
* @param text 字符串
* @param origin 起始位置
*
* @return 实例对象
*/
+ (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin; @end
//
// LabelView.m
// YXMWeather
//
// Created by XianMingYou on 15/2/16.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "LabelView.h" @interface LabelView () @property (nonatomic) CGFloat labelWidth;
@property (nonatomic) CGFloat labelHeight; @property (nonatomic, strong) UILabel *label; @end @implementation LabelView - (void)buildView {
// 设置label
self.label.text = self.text;
self.label.font = self.font;
self.label.textColor = self.textColor; // 获取宽度
self.labelWidth = [self.text widthWithLabelFont:self.font];
self.labelHeight = [self.text heightWithLabelFont:self.font withLabelWidth:MAXFLOAT];
self.label.width = self.labelWidth;
self.label.height = self.labelHeight; // 计算间距
self.label.x = self.gapFromLeft;
self.label.y = self.gapFromTop; // 重新设置尺寸
self.width = self.labelWidth + self.gapFromLeft + self.gapFromRight;
self.height = self.labelHeight + self.gapFromTop + self.gapFromBottom; // 设置背景色
if (self.color) {
self.backgroundColor = self.color;
}
} @synthesize label = _label;
- (UILabel *)label {
if (_label == nil) {
_label = [[UILabel alloc] initWithFrame:CGRectZero];
_label.textAlignment = NSTextAlignmentCenter;
[self addSubview:_label];
} return _label;
} + (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin {
LabelView *labelView = [[LabelView alloc] initWithFrame:CGRectMake(origin.x, origin.y, , )];
labelView.color = [UIColor blackColor];
labelView.text = text;
labelView.textColor = [UIColor whiteColor];
labelView.font = [UIFont fontWithName:LATO_BOLD size:];
labelView.gapFromLeft = .f;
labelView.gapFromRight = .f;
labelView.gapFromTop = .f;
labelView.gapFromBottom = .f; [labelView buildView]; return labelView;
} @end
使用时候的源码:
LabelView *labelView = [LabelView createWithText:@"YouXianMing" atOrigin:CGPointMake(10, 90)];
[self.view addSubview:labelView];
[控件] LabelView的更多相关文章
- Android开源库集合(控件)
RecycleView: RecycleView功能增强 https://github.com/Malinskiy/SuperRecyclerView RecycleView功能增强(拖拽,滑动删除, ...
- GitHub开源控件的使用合集
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- JS调用Android、Ios原生控件
在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...
- HTML5 progress和meter控件
在HTML5中,新增了progress和meter控件.progress控件为进度条控件,可表示任务的进度,如Windows系统中软件的安装.文件的复制等场景的进度.meter控件为计量条控件,表示某 ...
- 百度 flash html5自切换 多文件异步上传控件webuploader基本用法
双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核: 若页面需默认用极速核,增加标签:<meta name=& ...
- JS与APP原生控件交互
"热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...
- UWP开发必备:常用数据列表控件汇总比较
今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...
- 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~
一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...
随机推荐
- Linux运维中遇到的常见问题
1.CentOS启动tomcat出现乱码的解决方案1.打开tomcat下的server.xml配置文件,在connect标签中添加编码属性:URIEncoding="UTF-8"2 ...
- 自然语言处理--N-gram
考虑一个语音识别系统,假设用户说了这么一句话:“I have a gun”,因为发音的相似,该语音识别系统发现如下几句话都是可能的候选:1.I have a gun. 2.I have a gull. ...
- Pipenv——最好用的python虚拟环境和包管理工具
pipenv 是Kenneth Reitz大神的作品,能够有效管理Python多个环境,各种包.过去我们一般用virtualenv搭建虚拟环境,管理python版本,但是跨平台的使用不太一致,且有时候 ...
- eclipse 中文件引用报错不能编译,但引用文件确实存在
方法1:clean工程 方法2: 检查.classpath文件中该引用文件是否被排除在外.
- elasticsearch插件的开发--计算特征向量的相似度
目录 更改elasticsearch的score评分 插件源码解读 脚步一 脚本二(fast-vector-distance) 部署 测试 创建索引 查询 版本说明 项目详细见github 参考文献 ...
- 常用算法2 - 广度优先搜索 & 深度优先搜索 (python实现)
1. 图 定义:图(Graph)是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V,E),其中,G表示一个图,V是图G中顶点的集合,E是图G中边的集合. 简单点的说:图由节点和边组成.一 ...
- python学习之参数传递
^参数传递分为定义(形参)和调用(实参)两种情况.^ 1. 定义(形参) 默认参数 def func(x, y=None): # 任何时候必须 优先定义 位置参数 # 默认参数和可变参数*args 顺 ...
- C# 实现二叉树各种排序
1. 引言 在实际的项目中,树还是用的比较多的一种,尤其是对于具有层次结构的数据.相信很多人都学过树的遍历,比如先序遍历,后序遍历等,利用递归还是很容易理解的. 今天给大家介绍下二叉树的几种遍历算法, ...
- JVM GC总结
判断对象存活 引用计数算法 给对象中添加一个引用计数器,每当有一个地方引用它时,计数器就加1,引用失效时,计数器就减1:任何时刻计数器都为0的对象就是不可能再被使用的. 问题:无法解决对象之间的相互循 ...
- Mac Supervisor 管理进程
无论是在日常工作中还是平时玩代码中,我总是离不开 Supervisor,其实很久之前我就写过一篇文章:supervisord 部署 Flask,在里面,我仔细讲解了如何在 Linux 环境下安装并且配 ...