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 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 }}总结
以上是内存溢出为你收集整理的Swift - 计算文本高度全部内容,希望文章能够帮你解决Swift - 计算文本高度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)