iOS 计算字符串显示宽高度
ObjC(Category of NSString):
- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
CGSize resultSize = CGSizeZero;
if (self.length <= ) {
return resultSize;
}
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.lineBreakMode = NSLineBreakByWordWrapping;
resultSize = [self boundingRectWithSize:CGSizeMake(floor(size.width), floor(size.height))//用相对小的 width 去计算 height / 小 heigth 算 width
options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName: font,
NSParagraphStyleAttributeName: style}
context:nil].size;
resultSize = CGSizeMake(floor(resultSize.width + ), floor(resultSize.height + ));//上面用的小 width(height) 来计算了,这里要 +1
return resultSize;
}
Swift(Extension of NSString):
func getSizeWithFont(_ font:UIFont,constrainedToSize size:CGSize) -> CGSize{
if self.length == 0 { return CGSize.zero }
let style = NSMutableParagraphStyle.init()
style.lineBreakMode = .byWordWrapping
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSFontAttributeName:font,NSParagraphStyleAttributeName:style]
let bound = CGSize.init(width: floor(size.width), height: floor(size.height))
let rect = self.boundingRect(with: bound, options: options, attributes: attributes, context: nil)
let _size = CGSize.init(width: floor(rect.width + 1), height: floor(rect.height + 1))
return _size
}
iOS 计算字符串显示宽高度的更多相关文章
- iOS计算字符串的宽度高度
OC开发中会遇到根据字符串和字体大小来算计算出字符串所占的宽高->> 封装方法如下: #import <Foundation/Foundation.h> #import < ...
- iOS中计算字符串NSString的高度
根据固定宽度计算字符串高度: NSString *info = @"但是公司的高度是广东省公司的广东省高速度来开个大帅哥多撒谎个爱好就跟他说噶三公司噶是的刚好是我哥如果黑暗如果坏都干撒降低公 ...
- ios 计算字符串长度<转>
- (int)textLength:(NSString *)text//计算字符串长度 { float number = 0.0; for (int index = 0; index ...
- iOS计算富文本(NSMutableAttributedString)高度
有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ...
- ios计算字符串宽高,指定字符串变色,获取URL参数集合
#import <Foundation/Foundation.h> @interface NSString (Extension) - (CGFloat)heightWithLimitWi ...
- iOS: 计算 UIWebView 的内容高度
- (void)webViewDidFinishLoad:(UIWebView *)wb { //方法1 CGFloat documentWidth = [[wb stringByEvaluating ...
- iOS依据字符串计算UITextView高度
iOS计算字符串高度,有须要的朋友能够參考下. 方法一:ios7.0之前适用 /** @method 获取指定宽度width,字体大小fontSize,字符串value的高度 @param value ...
- 关于UIFont和计算字符串的高度和宽度
转自:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.创建方法:+ fontWithName:size:- fontWithSize:2.创建系统字体:+ sy ...
- iOS 动态计算文本内容的高度
关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. // 获取字符串的大小 ios6 - (CGSize)getStringRect_:(NSString* ...
随机推荐
- loj516 DP一般看规律(set启发式合并)
题目: https://loj.ac/problem/516 分析: 每次将一个颜色更改为另一个颜色相当于将两个集合合并 然后对于答案的更新,一个点插入到一个集合中,那么可能更新答案的就是其前驱节点或 ...
- Delphi中匿名方法动态绑定事件
应恢弘之约,写了一个对其发布的匿名函数动态绑定到事件的封装,代码如下: type TAnonEvent=class public class function Wrap<T1,T2>(On ...
- 【转】JavaScript错误处理和堆栈追踪
原文: https://www.cnblogs.com/caoru/p/6699583.html --------------------------------------------------- ...
- Eclipse luna 装不上 veloeclipse
今天在Eclipse中始终无法安装veloeclipse ,报错:Failed to prepare partial IU: [R]com.googlecode.veloeclipse.ui 2.0. ...
- HDU 2795 Billboard(宣传栏贴公告,线段树应用)
HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意: 要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要 ...
- centos7 配置虚拟交换机(物理交换机truckport设置)(使用brctl)
感谢朋友支持本博客.欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- 2016/2/25 onchange 应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU 5044 Tree 树链剖分+区间标记
Tree Problem Description You are given a tree (an acyclic undirected connected graph) with N nodes. ...
- a simple and universal interface between web servers and web applications or frameworks: the Python Web Server Gateway Interface (WSGI).
WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server comm ...
- mac toad下建表问题
mac toad下创建表,表名会自动多一个双引号,如:tb_test => "tb_test",这个应该是mac系统智能引号问题导致的,目前还没找到解决办法,只能手动用sql ...