Swift - 计算文本高度
发布时间:2020-12-14 06:15:53 所属栏目:百科 来源:网络整理
导读:http://www.cnblogs.com/YouXianMing/p/5823893.html //// String+StringHeight.swift// StringHeight//// Created by YouXianMing on 16/8/30.// Copyright ? 2016年 YouXianMing. All rights reserved.//import UIKitextension String { /** Get the heigh
http://www.cnblogs.com/YouXianMing/p/5823893.html // // String+StringHeight.swift // StringHeight // // Created by YouXianMing on 16/8/30. // Copyright ? 2016年 YouXianMing. All rights reserved. // import UIKit extension String { /** Get the height with the string. - parameter attributes: The string attributes. - parameter fixedWidth: The fixed width. - returns: The height. */ func heightWithStringAttributes(attributes : [String : AnyObject],fixedWidth : CGFloat) -> CGFloat { guard self.characters.count > 0 && fixedWidth > 0 else { return 0 } let size = CGSizeMake(fixedWidth,CGFloat.max) let text = self as NSString let rect = text.boundingRectWithSize(size,options:.UsesLineFragmentOrigin,attributes: attributes,context:nil) return rect.size.height } /** Get the height with font. - parameter font: The font. - parameter fixedWidth: The fixed width. - returns: The height. */ func heightWithFont(font : UIFont = UIFont.systemFontOfSize(18),fixedWidth : CGFloat) -> CGFloat { guard self.characters.count > 0 && fixedWidth > 0 else { return 0 } let size = CGSizeMake(fixedWidth,attributes: [NSFontAttributeName : font],context:nil) return rect.size.height } /** Get the width with the string. - parameter attributes: The string attributes. - returns: The width. */ func widthWithStringAttributes(attributes : [String : AnyObject]) -> CGFloat { guard self.characters.count > 0 else { return 0 } let size = CGSizeMake(CGFloat.max,0) let text = self as NSString let rect = text.boundingRectWithSize(size,context:nil) return rect.size.width } /** Get the width with the string. - parameter font: The font. - returns: The string's width. */ func widthWithFont(font : UIFont = UIFont.systemFontOfSize(18)) -> CGFloat { guard self.characters.count > 0 else { return 0 } let size = CGSizeMake(CGFloat.max,context:nil) return rect.size.width } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- PostgreSQL学习笔记7之函数和操作符<三>
- oracle – ODP.NET托管ConnectionPool每3分钟关闭/打开一次
- c# – 如何从Cell_Leave事件中获取DataGridViewCell的值?
- C .为什么std :: cout << char int打印int值?
- postgresql – Postgrex – 主机“xxx.xxx.xxx.xxx”没有pg
- 两种高性能I/O设计模式(Reactor/Proactor)的比较
- 如何编写nand flash 驱动
- C#遍历得到checkboxlist选中值和设置选中项的代码
- C#使用windows服务开启应用程序的方法
- C#在MYSQL中存储DateTime到Timestamp列