[控件] 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 = 0;if (self.length == 0) {height = 0;} else {// 字体NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};if (font) {attribute = @{NSFontAttributeName: font};}// 尺寸CGSize retSize = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext:nil].size;height = retSize.height;}return height; }- (CGFloat)widthWithLabelFont:(UIFont *)font {CGFloat retHeight = 0;if (self.length == 0) {retHeight = 0;} else {// 字体NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};if (font) {attribute = @{NSFontAttributeName: font};}// 尺寸CGSize retSize = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext: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 {// 设置labelself.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, 0, 0)];labelView.color = [UIColor blackColor];labelView.text = text;labelView.textColor = [UIColor whiteColor];labelView.font = [UIFont fontWithName:LATO_BOLD size:8];labelView.gapFromLeft = 10.f;labelView.gapFromRight = 10.f;labelView.gapFromTop = 2.f;labelView.gapFromBottom = 2.f;[labelView buildView];return labelView; }@end
使用时候的源码:
LabelView *labelView = [LabelView createWithText:@"YouXianMing" atOrigin:CGPointMake(10, 90)];
[self.view addSubview:labelView];
[控件] LabelView相关推荐
- 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照)
转载请注明出处:王亟亟的大牛之路 P1:这部分为废话技术内容在P2,不想看的可跳过 最近每天都是在照顾鱼,麦麦,当当之间游离回家几乎没学习,没敲代码,或者说没开工作电脑,慢慢的罪恶感,贴两张周末小朋友 ...
- labelview标签列表控件的使用介绍
标签列表控件的使用介绍 支持点击事件监听 step1: D:\workspace\LabelViewDemoTwo\app\src\main\res\values\strings.xml <re ...
- GitHub开源控件的使用合集
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Android开源库集合(控件)
RecycleView: RecycleView功能增强 https://github.com/Malinskiy/SuperRecyclerView RecycleView功能增强(拖拽,滑动删除, ...
- android常用控件实验报告,ui设计实验报告.doc
ui设计实验报告 ui设计实验报告 篇一:UI设计实验报告 实验项目四:UI设计 一. 实验目的和要求 1.熟练运用Eclipse软件中的swing设计. 2.掌握UI编写的软件. 3.能都熟练的进行 ...
- Android 控件之Gallery图片集
Gallery是Android中的图片库控件.先看效果,爽一番 源码下载 一.简介 在中心锁定,水平显示列表的项. 二.实例 1.布局文件 <?xml version="1. ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Andoird控件收集
Android UI相关开源项目库汇总 OpenDigg 抽屉菜单 MaterialDrawer ★7337 - 安卓抽屉效果实现方案 Side-Menu.Android ★3865 - 创意边侧菜单 ...
- Qt---布局,设置控件边距,拉伸因子
QGridLayout *LeftLayout =new QGridLayout(this); LeftLayout->addWidget(label1, 0, 0); //label1在第1行 ...
最新文章
- Binary Tree Nodes(单表多实例查询)
- Maven 编译使用 rt.jar
- 《Kali+Linux渗透测试的艺术》学习总结之----Kali Linux简介
- 剑指offer:二叉搜索树的第k个结点(中序遍历)
- js学习总结----js中常用的四种输出方式
- 【李宏毅2020 ML/DL】P88-96 Meta Learning – MAML | Reptile
- 奥特曼传奇英雄存档丢了怎么找回_热血传奇复古传奇:传奇游戏手机版竟然比端游还火爆?你觉得呢?...
- Windows10下鼠标跳屏问题——Microsoft Serial Ballpoint
- Ubuntu 11.10 系统启动默认进入终端
- php number_format 通过千位分组来格式化数字
- 项目五dns服务器实训,DNS服务器实训报告.doc
- 【Axure原型分享】自动编号的中继器表格
- 记录一次获取车载摄像头数量为0同时打开摄像头黑屏的问题分析(基于Android M)
- TeamViewer和远程桌面冲突的问题
- 当前 .NET SDK 不支持将 .NET Core 2.2 设置为目标。请将 .NET Core 2.1 或更低版本设置。
- 奶茶店一天盈利有多少?广州哪里有专业奶茶培训点
- JavaScript中的扁平化数据转换为树形结构、树形结构扁平化数据
- vue krpano 视角监听
- 前端证券项目_富途证券WEB前端团队招募令
- 鼠标悬停显示滚动条,移出不显示
热门文章
- 2015 跨年博文总结
- Java 生产者和消费者问题
- $.ajax()常用方法详解(推荐)
- python之路-day18-反射
- php_mvc实现步骤五
- MVC控制器传递多个实体类集合到视图的方案总结
- hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】
- 解决 项目cocoapods diff: /../Podfile.lock: No such file or directory
- Java经典实例:比较浮点数
- NOIP2009普及组细胞分裂(数论)——yhx