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的更多相关文章

  1. Android开源库集合(控件)

    RecycleView: RecycleView功能增强 https://github.com/Malinskiy/SuperRecyclerView RecycleView功能增强(拖拽,滑动删除, ...

  2. GitHub开源控件的使用合集

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  3. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  4. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  5. HTML5 progress和meter控件

    在HTML5中,新增了progress和meter控件.progress控件为进度条控件,可表示任务的进度,如Windows系统中软件的安装.文件的复制等场景的进度.meter控件为计量条控件,表示某 ...

  6. 百度 flash html5自切换 多文件异步上传控件webuploader基本用法

    双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核: 若页面需默认用极速核,增加标签:<meta name=& ...

  7. JS与APP原生控件交互

    "热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...

  8. UWP开发必备:常用数据列表控件汇总比较

    今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...

  9. 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~

    一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...

随机推荐

  1. @Override 注解compiler1.5和compiler1.6不同

    说到注解问题,@interface 来定义注解类 这个注解出现是在jdk1.5版本出现. jdk1.5只支持@override对类的继承中方法重写的使用,不支持接口实现中方法重写的使用(这种情况下会报 ...

  2. java学习-struts基础(一)

    struts发展 struts是Apache软件基金会赞助的一个开源项目,是一个基于Java EE的MVC开源实现. 它为Servlet/JSP技术的应用提供技术框架2001.7--Struts1正式 ...

  3. php的数组变量

    数组就是存储同一类型的多个变量的 一种特殊的类型 php的数组有两种形态 1.普通类型 eg:$cars = array("Volvo","BMW"," ...

  4. Servlet多文件上传

    各位大侠可能会对263电子邮箱中的"上传附件"功能有印象,就是:在浏览 器中点击"浏览",弹出一个对话框,选中文件后,单击"确定",文件就被 ...

  5. 在Struts2标签s:textfield中显示正确的日期

    Java代码   struts2中的日期期输入显示问题   struts2 中的默认的日期输出并不符合我们的中文日常习惯.以下是我知道的在struts2中进行日期格式化输出的几种方式. 1.利用 &l ...

  6. elasticsearch(一):JAVA api操作

    1.创建一个mavan项目,项目的以来配置如下. <?xml version="1.0" encoding="UTF-8"?> <projec ...

  7. 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)

    需求背景 利用线程来模拟生产者和消费者模型 系统建模 这个系统涉及到三个角色,生产者,消费者,任务队列,三个角色之间的关系非常简单,生产者和消费者拥有一个任务队列的引用,生产者负责往队列中放置对象(i ...

  8. Angular2 获取当前点击的元素

    <a (click)="onClick($event)"></a> onClick($event){ console.log($event.target); ...

  9. 解决部分小程序无法获取UnionId的问题

    问题背景 通过观察数据,发现有一部分用户是无法获取到UnionId的 也就是接口返回的参数中不包含UnionId参数 看了微信文档的解释,只要小程序在开放平台绑定,就一定会分配UnionId 网上也有 ...

  10. CentOS6.8启动Tomcat无法访问

    今天笔者在CentOS6.8的生产环境上配置Java环境,安装JDK,部署Tomcat,这本来是很简单的一件事,可是最后发现通过IP一直访问不了Tomcat的默认页面. 图1. 无法访问Tomcat默 ...