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. apktool更新,JDK升级配置

    最近使用apktool反编译apk中的xml文件总是失败. Exception in thread "main" brut.androlib.AndrolibException: ...

  2. UVAlive3486_Cells

    给一棵树,每次每次询问一个点是否是另一个点的祖先? 首先,题目的读入就有点坑爹,注意,每个节点的值是说明它下面有多少个儿子节点,直接对于每个下标保存一个值即可. 对于查询是否是祖先,我们可以对于每一个 ...

  3. java byte[]生成

    1. ByteArrayOutputStream extends OutputStream 提供了一个byte数组,和记录写入数组值个数的类. a.实现了write(int)这个抽象函数,这里默认只写 ...

  4. EF 实践

    一.CodeFirst 1.新建MVCEFManger(业务层)项目 在该项目下引入entityframework 安装包(用来生成数据库或者实体) PM> install-package en ...

  5. 数论 : 模运算法则(poj 1152)

    题目:An Easy Problem! 题意:求给出数的最小进制. 思路:暴力WA: discuss中的idea: 给出数ABCD,若存在n 满足 (A* n^3 +B*n^2+C*n^1+D*n^0 ...

  6. 非官方windows下Cpython二进制扩展包下载地址

    Unofficial Windows Binaries for Python Extension Packages url:http://www.lfd.uci.edu/~gohlke/pythonl ...

  7. maven打包时使用的pom配置

    <build> <plugins> <!-- compiler插件, 设定JDK版本 --> <plugin> <groupId>org.a ...

  8. MySQL主从复制、半同步复制和主主复制

    同步,异步,半同步复制的比较: 同步复制:Master提交事务,直到事务在所有的Slave都已提交,此时才会返回客户端,事务执行完毕.缺点:完成一个事务可能会有很大的延迟. 异步复制:当Slave准备 ...

  9. Ajax无刷新分页

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxPage ...

  10. hdu 5101 n集合选2个不同集合数使和大于k

    http://acm.hdu.edu.cn/showproblem.php?pid=5101 给n个集合,选择两个来自不同集合的数,加和大于k,问有多少种选择方案. 答案=从所有数中选择的两个加和大于 ...