[控件] 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'所深深吸引,自从有了它,麻 ...
 
随机推荐
- 回退Ubuntu记录
			
前言 由于Ubuntu18经常出错,因而决定回退Ubuntu16,下面是记录回退问题及美化,以便以后需要. 问题总结 磁盘挂载 挂载其他磁盘分区时,提示错误"Metadata kept in ...
 - C#中通过Lambda表达式为委托传入更多的参数
			
如: DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += (o, e) => { d ...
 - Hive和SparkSQL: 基于 Hadoop 的数据仓库工具
			
Hive: 基于 Hadoop 的数据仓库工具 前言 Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的 SQL 查询功能,将类 SQL 语句转 ...
 - 网络爬虫(一):配置selenium、pycharm(windows平台)
			
最近在学习爬虫的编写,使用selenium模块时候,遇到了很多坑,本blog的目的是总结一下遇到的坑和解决办法,以便后来人少走弯路! 以下介绍均以Python3.x为基准进行,基于windows平台的 ...
 - Java操作elasticsearch
			
使用 Maven 工程,我的 pom 文件如下所示: <dependencies> <dependency> <groupId>org.elasticsearch& ...
 - C学习笔记(2)--指针
			
一.多文件结构总结 1.子源文件里面包含自己对应的头文件 2.无论是何源文件调用库函数,都需要包含该库函数的声明所在的头文件 3.头文件又叫接口文件,.c对数据和函数进行封装和包含, .h就是.c对外 ...
 - WPF 从文件加载字体
			
本文告诉大家从文件加载字体.在wpf 使用 fontfamily 显示指定的 ttf 显示字体 假如有字体在 C:\Projects\MyProj\free3of9.ttf ,可以使用 Private ...
 - 转载:SQL中Group By 的常见使用方法
			
SQL中Group By 的常见使用方法 转载源:http://www.cnblogs.com/wang-meng/p/5373057.html 前言今天逛java吧看到了一个面试题, 于是有了今天 ...
 - [日常] 搭建golang开发环境
			
下载目录:https://studygolang.com/dl32位选 go1.10.linux-386.tar.gz64位选 go1.10.linux-amd64.tar.gz uname -a查看 ...
 - .NET Core 2.1路线图
			
Microsoft的Scott Hunter发布了Microsoft .NET Core 2.1版本的路线图.Hunter宣布Microsoft .NET Core每天约有五十万开发人员的使用量.根据 ...