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. 如何在 IIS 中设置 HTTPS 服务

    Windows Server2008.IIS7启用CA认证及证书制作完整过程 这篇文章介绍了如何安装证书申请工具: 如何在iis创建证书申请: 如何使用iis申请证书生成的txt文件,在工具中开始申请 ...

  2. java动态加载类和静态加载类笔记

    JAVA中的静态加载类是编译时刻加载类  动态加载类指的是运行时刻加载类 二者有什么区别呢 举一个例子  现在我创建了一个类  实现的功能假设为通过传入的参数调用具体的类和方法 class offic ...

  3. 线程学习笔记(EventWaitHandler)AutoResetEvent的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. java中throw和throws的区别

    throw和throws的区别: throws 用在方法声明后面,跟的是异常类名 可以跟多个异常类名,用逗号隔开 表示抛出异常,由该方法的调用者来处理 throws表示出现异常的一种可能性,并不一定会 ...

  5. 【html】:禁止鼠标事件

    <body oncontextmenu="return false" onselectstart="return false" ondragstart=& ...

  6. Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]

    作业提交时间:10月9日上课前. Design and implement an Elevator Scheduler to aim for both correctness and performa ...

  7. Struts2中请求参数的接收方式和ModelDriven机制及其运用

    odelDriven 为什么需要ModelDriven 所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar ...

  8. 冒泡排序优化JAVA

    本文对传统的冒泡排序进行了一些优化,减少了循环次数. 时间复杂度 若文件的初始状态是正序的,一趟扫描即可完成排序.所需的关键字比较次数 C 和记录移动次数 M 均达到最小值: C(min)=n-1 , ...

  9. 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values

    对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...

  10. C#设计模式(13)——代理模式(Proxy Pattern)

    一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...