NSString(或者说是UILabel)加入 “行间距” 之后的 “高度”计算
一、首先,写一个工具类(NSString的分类,增加两个功能,计算高度宽度)
#import "NSString+Extension.h"
@implementation NSString (Extension)
#pragma mark -根据宽度,字号来计算字符串的高度
- (float) heightWithFont: (UIFont *) font withinWidth: (float) width{
CGRect textRect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:font}
context:nil];
return ceil(textRect.size.height);
} #pragma mark -根据字号来计算字符串的宽度
- (float) widthWithFont: (UIFont *) font{
CGRect textRect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, font.pointSize) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:font}
context:nil];
return ceil(textRect.size.width);
}
二、为了适配不同系统,字体字号作如下封装:
/*
* 自定义字号大小
*/
-(UIFont *)jyeFontWithName:(NSString *)name size:(CGFloat)size{
UIFont * font = nil;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= ){
font = [UIFont fontWithName:name size:size];
}else{ if([name rangeOfString:@"Medium"].location == NSNotFound){
font = [UIFont systemFontOfSize:size];
}else{
font = [UIFont boldSystemFontOfSize:size];
}
}
return font;
}
三、在需要计算字符串高度宽度的类中,做如下处理:
NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5mg,叶酸200μgDFE,维生素D 10ug,维生素B1 1.2mg,维生素B2 1.2mg。";
UIFont *font = [self jyeFontWithName:@"PingFangSC-Regular" size:];//需要与UILabel的text保持一致
CGFloat oneRowHeight = [@"test" sizeWithAttributes:@{NSFontAttributeName:font}].height;//test 只是随便取得一个字符串,只要不超过一行就可以
CGFloat textHeight = [string heightWithFont:font withinWidth:self.frame.size.width-];
CGFloat rows = textHeight/oneRowHeight;
CGFloat realHeight = (rows *ceilf(oneRowHeight)) +(rows - )* LineSpace;//行间距 为4
//realHeight 就是字符串的高度啦,此时你就可以根据交互设计图编写相应的UILabel的frame啦
四、UILabel 设置行间距
UILabel * hotLabel = [[UILabel alloc]init];
hotLabel.frame = CGRectMake(, , sectionView.frame.size.width - , realHeight);
hotLabel.textColor = JYEColor(, , );
hotLabel.numberOfLines = ;
hotLabel.font = [self jyeFontWithName:@"PingFangSC-Regular" size:]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range = [string rangeOfString:@"今日营养配餐"];
[attributedString addAttribute:NSForegroundColorAttributeName value:JYEColor(, , ) range:range];
[attributedString addAttribute:NSFontAttributeName value:[self jyeFontWithName:@"PingFangSC-Medium" size:] range:range];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:];//调整行间距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, [string length])]; hotLabel.attributedText = attributedString;
NSString(或者说是UILabel)加入 “行间距” 之后的 “高度”计算的更多相关文章
- (转载)iOS UILabel自定义行间距时获取高度
本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...
- iOS UILabel自定义行间距时获取高度
本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...
- iOS 设置UILabel的行间距并自适应高度
NSString *contentStr = @"总以为,在最初的地方,有一个最原来的我,就也会有一个最原来的你"; UILabel *tempLabel = [[UILabel ...
- UILabel设置行间距和字间距并计算高度-b
#define UILABEL_LINE_SPACE 6 #define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height //给UILabe ...
- (转)iOS学习——UIlabel设置行间距和字间距
在iOS开发中经常会用到UIlabel来展示一些文字性的内容,但是默认的文字排版会觉得有些挤,为了更美观也更易于阅读我们可以通过某些方法将UIlabel的行间距和字间距按照需要调节. 比如一个Labe ...
- 修改UILabel的行间距
在iOS开发中 有时候为了调整一些UI效果 我们需要调整UILabel之间的行间距: contentLabel.text:label上显示的文字内容; 5:label行间距; contentLab ...
- UILabel设置行间距
UILabel设置行间距: NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWi ...
- label_设置行距、字距及计算含有行间距的label高度
// // ViewController.m // CNBlogs // // Created by PXJ on 16/5/27. // Copyright © 2016年 PXJ. All ...
- IOS开发调整UILabel的行间距
CGFloat heih = 20; NSString * cLabelString = @"这是测试UILabel行间距的text.这是测试UILabel行间距的text.n 这是测试 ...
随机推荐
- ural 1142. Relations
1142. Relations Time limit: 1.0 secondMemory limit: 64 MB Background Consider a specific set of comp ...
- ccc 使用let
//如果不是恩雅,也不在移动过程中,那么移动 if (!self.hasMoved && !isHold) { var touchLoc = touch.getLocation(); ...
- BZOJ 1925[Sdoi2010]地精部落 题解
题目大意: 1~n的排列中,要任意一个数要么比它左右的数都大或小,求所有的方案数. 思路: 主要思路:离散. 三个引理: ①在n->n-1的转化过程中,我们删除了一个点后,我们可以将n-1个点视 ...
- ACM Coin Test
Coin Test 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 As is known to all,if you throw a coin up and let ...
- 『备忘』HttpWebRequest 在 POST 提交时, 标头(Headers)丢失原因
近来研究 HttpWebRequest —— 辅助类完成时,POST JSON数据 总会 丢失标头(Headers). HttpWebRequest POST JSON数据,分如下几步: > 将 ...
- 【BZOJ】2659: [Beijing wc2012]算不出的算式
题意 给两个奇质数\(p, q(p, q < 2^{31})\),求\(\sum_{k=1}^{\frac{p-1}{2}} \left \lfloor \frac{kq}{p} \right ...
- 【HDU】2138 How many prime numbers
http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...
- UICollectionView集合视图的概念
如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...
- UE编辑器FTP无法连接
解决办法:http://wenwen.sogou.com/z/q197743020.htm 无法从ue连接到主机,一直就是这样的状态 1. ftp帐户密码都没有问题: 2. 后台主机也没有问题: 3. ...
- django1.9 创建数据表
1.在setting.py 中注册app: 2.编写models.py 文件创建表结构: (生成的表默认是: app名称_定义的表面 ) 3.执行命令: python manage.py check ...