Swift - 计算文本高度

效果

源码

//
// 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 > && fixedWidth > else { return
} 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(), fixedWidth : CGFloat) -> CGFloat { guard self.characters.count > && fixedWidth > else { return
} let size = CGSizeMake(fixedWidth, CGFloat.max)
let text = self as NSString
let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, 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 > else { return
} let size = CGSizeMake(CGFloat.max, )
let text = self as NSString
let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, 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()) -> CGFloat { guard self.characters.count > else { return
} let size = CGSizeMake(CGFloat.max, )
let text = self as NSString
let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: [NSFontAttributeName : font], context:nil) return rect.size.width
}
}

Swift - 计算文本高度的更多相关文章

  1. swift - 动态计算文本高度

        func heightOfCell(text : String) -> CGFloat {        let attributes = [NSFontAttributeName:UI ...

  2. Swift计算文本宽高

    iOS 8 开始可以配合 AutoLayout 自动估算文本的高度,但是当 Cell 比较复杂的时候,还会需要手动去计算.首先声明一个样式 var TextStyle : [String : NSOb ...

  3. iOS计算文本高度

    NSDictionary *attribute = @{NSFontAttributeName: UIFont(14)}; CGRect labelRect = [string boundingRec ...

  4. iOS学习之根据文本内容动态计算文本框高度的步骤

    在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...

  5. 精简计算UITableView文本高度

    精简计算UITableView文本高度 本人视频教程系类   iOS中CALayer的使用 最终效果: 核心源码(计算文本高度的类) NSString+StringHeight.h 与 NSStrin ...

  6. iOS6 以上设置文本高度,行高(转)

    2013-12-09     我来说两句   来源:冻僵的企鹅'zone   收藏    我要投稿 在iOS 7之前,常用下面这个方法计算文本高度sizeWithFont:constrainedToS ...

  7. iOS开发——文本高度

    1.简单的计算文本高度 // 要计算的文本内容 NSString *testString = @"刘成利,软件工程专业毕业,iOS开发者,目前工作于北京,在证券金融领域从事iOS App开发 ...

  8. iOS 动态计算文本内容的高度

    关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. //  获取字符串的大小  ios6 - (CGSize)getStringRect_:(NSString* ...

  9. swift计算label动态宽度和高度

    swift计算label动态宽度和高度 func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let ...

随机推荐

  1. 阿里云配置nginx+php+mysql

    #yum -y install php mysql mysql-server mysql-devel php-mysql php-cgi php-mbstring php-gd php-fastcgi ...

  2. Pig Hive对比(zz)

    Pig Latin:数据流编程语言 一个Pig Latin程序是相对于输入的一步步操作.其中每一步都是对数据的一个简单的变换. 用Pig Latin编程更像在RDBMS中“查询规划器”(query p ...

  3. js中json对象的深拷贝

    /** * 对象深拷贝 * @param obj */ function deepCopy(obj) { var txt=JSON.stringify(obj); return JSON.parse( ...

  4. 一个使用方便的对FMDB进行封装的框架和一个可以切应用图标的应用

    框架的git地址 :https://github.com/Joker-King/JKDBModel 切割图标的应用地址 http://pan.baidu.com/s/1kVjflwr

  5. 《UML大战需求分析》阅读笔记2

    在第三章往后的章节里面,作者着重描述了uml各个图的具体用法,首先则是类图. 类图则表示程序中所出现的用到的类,用方框来表示,方框中分为三行,第一行是类的名字,第二行是类的成员变量(属性),第三行是成 ...

  6. HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)

    题意:给定一个1-n的墙,然后有两种操作,一种是P l ,r, a 把l-r的墙都染成a这种颜色,另一种是 Q l, r 表示,输出 l-r 区间内的颜色. 析:应该是一个线段树+状态压缩,但是我用s ...

  7. C++ 无法将值写入注册表 HKEY_LOCAL_MACHINE\Sofeware\Microsoft\Windows\CurrentVersion\Run,以实现开机自启动应用程序。

    C++  无法将值写入注册表 HKEY_LOCAL_MACHINE\Sofeware\Microsoft\Windows\CurrentVersion\Run,以实现开机自启动应用程序.但可以写入到H ...

  8. 6.7 Binder机制

    Binder在Android系统中江湖地位非常之高.在Zygote孵化出system_server进程后,在system_server进程中出初始化支持整个Android framework的各种各样 ...

  9. iOS开发零基础--Swift篇:逻辑分支

    一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...

  10. hdu 4648 - Magic Pen 6(“水”题)

    摘自题解: 题意转化一下就是: 给出一列数a[1]...a[n],求长度最长的一段连续的数,使得这些数的和能被M整除. 分析: 设这列数前i项和为s[i], 则一段连续的数的和 a[i]+a[i+1] ...