[控件] 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'所深深吸引,自从有了它,麻 ...
随机推荐
- ActiveMQ消息持久化到Mysql数据库
1.把连接MySQL数据库的jar文件,放到ActiveMQ的lib目录下 2.修改ActiveMQ的conf目录下的activemq.xml文件,修改数据持久化的方式2.1 修改原来的kahadb的 ...
- mongodb二进制安装与yum安装
一.什么是mongodb MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当 ...
- C#的Equals不区分大小写
代码:System.Windows.Forms.MessageBox.Show("abc".Equals("ABC", StringComparison.Inv ...
- JavaScript函数——预编译
四部曲 创建AO对象 找形参和变量声明,将变量和形参名作为AO属性名,值为undefined. 将实参值和形参值统一 在函数体内找函数声明,值赋予函数体. 权重按顺序依次增加.以下例子即可体现上述规则 ...
- Visual Basic了解
Visual Basic是一种由微软公司开发的结构化的.模块化的.面向对象的.包含协助开发环境的事件驱动为机制的可视化程序设计语言.这是一种可用于微软自家产品开发的语言.它源自于Basic编程语言.V ...
- SQLServer数据库循环表操作每一条数据(游标的使用)
DECLARE @FunctionCode VARCHAR(20)--声明游标变量DECLARE curfuntioncode CURSOR FOR SELECT FunctionalityCode ...
- 对于移动端浏览器touch事件的研究总结(4)判断手指滑动方向
最近有一些微信的项目,虽然页面很简单,但配合手势后的效果却是很不错的.最基本的效果就是手指向上滑,页面配合css3出现一个展开效果,手指向下滑将展开的内容按原路径收起.其实就是一个简单的判断手指滑动方 ...
- anglar JS使用两层ng-repeat嵌套使用,分辨$index
使用ng-init给首层的每个元素赋值一个独立的值. ng-init="outerIndex = $index;" HTML: <div class="catego ...
- CassiniDev源码学习 - 可替代IIS的单机Web Form解决方案
最近一个项目是将web版的程序,改为单机版.话说这个web版号称当年十几个人用了至少3个月的时间开发,后来三年还不断有修改,而现在要在1个月内由一个人完成,这简直是不可能完成的任务!直觉告诉我,重写肯 ...
- vue三要素及底层实现机制
深入解析Vue 我们首先来熟悉一下我们这个文档所学习内容的流程. 先对比一下jQuery和Vue的区别,再讲述Vue的MVVM模型,接着讲解Vue的实现流程. 当然,我是不相信没有对比哪来的伤害,没有 ...