[控件] 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行 ...
最新文章
- webpack加载器打包样式表中的图片和字体
- 淡入淡出效果 || 高亮显示案例
- 从源代码的角度分析--在BaseAdapter调用notifyDataSetChanged()之后发生了什么
- ASP.NET常用代码
- 深度学习中的损失函数总结以及Center Loss函数笔记
- gcc 编译流程分析
- Linux之SWIG安装(无需安装pcre依赖)
- 同为开发:有了分布式/高并发等这些Java项目经历,面大厂稳了!
- could not be installed at this time
- 转 json数组对象和对象数组
- WPF中的Application类。
- vc2010解决方案项目编译顺序_安装及配置 VC2010 的详细步骤
- 21世纪的文件系统:概述WindowsNT 5.0文件系统(NTFS)(三)
- SQLite读写同步之WAL机制
- 「Wekan」- 看板工具 @20210403
- win10镜像文件能直接安装吗
- 云服务器的带宽是什么意思?怎么选择带宽大小?
- 华硕固件默认ip,不能进入路由的管理后台了?千万别慌张
- 服务器显示ipv4有两个ip地址,ipconfig命令后为什么出现两个IPV4地址?
- magic3是鸿蒙系统吗,如果荣耀Magic3搭载屏下镜头和鸿蒙系统,你会做第一批吗?...
热门文章
- sqlite 日期字段操作
- 3PAR推InServ-T级存储 EMC们紧张了?
- 每个开发人员现在应该下载的十种必备工具
- [1.1]XMI 与UML结合开发企业应用中业务模型
- 动手开发一个简易的 PHP for Git Server 第一章
- 在创建表时,对于auto_now=True与 auto_now_add=True 的区别
- Linux_基础_进程管理
- #22. 【UR #1】外星人
- 用java来实现验证码功能
- linux —— 学习笔记(用户管理与权限控制)