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. 小希的数表2(用的for循环)

    #include<iostream> 02.#include<algorithm> 03.using namespace std; 04. 05.bool cmp(int a, ...

  2. Azure IOT (EventHub + Stream Analytics + Table Storage)的使用

    最近研究利用Azure的 Event hub ,Stream Analytics和TableStorage来实现IOT的一个方案, 利用Event hub来采集传感器设备数据值,然后输入到Stream ...

  3. 对比DOM和jQuery完善度

    <input type="text" id="username" value="请输入你的用户名"> <script> ...

  4. Java学习笔记 第一章 入门<转>

    第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...

  5. PHP疑惑

    <?php $a = array(); $a[0] = 1; $a[1] = 2; $b = (object)$a; var_dump($b); 怎么从对象$b 中取值??? <?php ...

  6. matlab微分方程dsolve使用

    y=dsolve('Dy=exp(-x-y-2)','y(0)=-2','x') dy/dx 写成Dy (注意大小写) y(0)=-2 表示初始条件 'x'表示积分变量

  7. jquery实现TODOList

    html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  8. git --如何撤销已放入缓存区(Index区)的修改

    修改或新增的文件通过 git add --all 命令全部加入缓存区(index区)之后,使用 git status 查看状态(git status -s 简单模式查看状态,第一列本地库和缓存区的差异 ...

  9. Splunk及splunkforward简单部署配置

    部署环境 操作系统 服务器操作系统版本:CentOS release 6.5 (Final) 2.6.32-431.el6.x86_64 软件 软件版本:splunk-6.4.0 tar: splun ...

  10. C常用数据类型长度

    1.整型数据类型 2.无符号整型数据类型 3.字符型数据类型 char  字节数  1: 4.浮点型数据类型