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. jdk1.8以前不建议使用其自带的Base64来加解密

    JDK1.8之前的base64是内部测试使用的代码,不建议生产环境使用,而且未来可能会移除, JDK1.8提供最新可以正式使用的Base64类, 不要使用JDK中自带的sun.misc.BASE64D ...

  2. nlp 总结 分词,词义消歧,词性标注,命名体识别,依存句法分析,语义角色标注

    分词 中文分词 (Word Segmentation, WS) 指的是将汉字序列切分成词序列. 因为在汉语中,词是承载语义的最基本的单元.分词是信息检索.文本分类.情感分析等多项中文自然语言处理任务的 ...

  3. Go 单元测试、基准测试、并发基准测试

    一.单元测试 要开始一个单元测试,需要准备一个 go 源码文件,在命名文件时需要让文件必须以_test结尾. 单元测试源码文件可以由多个测试用例组成,每个测试用例函数需要以Test为前缀,例如: fu ...

  4. Eth 部署智能合约

    首先要开发以太坊的智能合约,需要EVM(以太坊虚拟机),也就是需要运行的环境,我们可以通过 geth 来设置开发环境: geth --datadir testNet --dev console 2&g ...

  5. C#的OpenFileDialog的简单用法

    1.OpenFileDialog 中文名字叫做 打开文件对话框 OpenFileDialog的效果如图: private void btnSelectFile_Click(object sender, ...

  6. window.history的跳转实质-HTML5 history API 解析

    在上一浏览器跳转行为的测试中,我们看到了通过不同的方法操作浏览器跳转时,它的刷新表现有所不同,在这一文章中,将看看,为何会产生这样的不同?其背后的实质是什么?浏览器的访问历史记录到底是如何运作的呢? ...

  7. SpringMVC源码阅读:定位Controller

    1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码分析,弄清楚Spr ...

  8. 二维码之qrencode生成(带logo)

    从github下载的qrencode没有QRCodeGenerator文件,需要引入 // // QR Code Generator - generates UIImage from NSString ...

  9. 快速安装.net 4.0

    1.打开运行输入 cmd 2.输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 3.输入 aspnet_regiis.exe -i

  10. Dev中自带添加、编辑、删除等按钮的文字颜色等修改

    下面是ASPxGridView的自带按钮的文字等修改 <SettingsCommandButton> <NewButton Text=" " Image-Tool ...