Swift - 计算文本高度
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 - 计算文本高度的更多相关文章
- swift - 动态计算文本高度
func heightOfCell(text : String) -> CGFloat { let attributes = [NSFontAttributeName:UI ...
- Swift计算文本宽高
iOS 8 开始可以配合 AutoLayout 自动估算文本的高度,但是当 Cell 比较复杂的时候,还会需要手动去计算.首先声明一个样式 var TextStyle : [String : NSOb ...
- iOS计算文本高度
NSDictionary *attribute = @{NSFontAttributeName: UIFont(14)}; CGRect labelRect = [string boundingRec ...
- iOS学习之根据文本内容动态计算文本框高度的步骤
在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...
- 精简计算UITableView文本高度
精简计算UITableView文本高度 本人视频教程系类 iOS中CALayer的使用 最终效果: 核心源码(计算文本高度的类) NSString+StringHeight.h 与 NSStrin ...
- iOS6 以上设置文本高度,行高(转)
2013-12-09 我来说两句 来源:冻僵的企鹅'zone 收藏 我要投稿 在iOS 7之前,常用下面这个方法计算文本高度sizeWithFont:constrainedToS ...
- iOS开发——文本高度
1.简单的计算文本高度 // 要计算的文本内容 NSString *testString = @"刘成利,软件工程专业毕业,iOS开发者,目前工作于北京,在证券金融领域从事iOS App开发 ...
- iOS 动态计算文本内容的高度
关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. // 获取字符串的大小 ios6 - (CGSize)getStringRect_:(NSString* ...
- swift计算label动态宽度和高度
swift计算label动态宽度和高度 func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let ...
随机推荐
- 第一章 git指令与设置
相关指令: 1.从远程的master分支上创建新的分支,此时新分支内容与master分支内容相同: git checkout master; git branch newbranch; git che ...
- dropbear
生成ssh连接所需要的公钥,如下: /usr/bin/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key (dss加密,长度默认 ...
- Jquery点击发送按钮后,按钮文本倒计时
1.html代码 <input type="number" id="mobileNo" placeholder="请输入手机号" /& ...
- SQLServer 脚本测试
最近在做大数据同步的工作.很少数据需要特殊清洗算法,每次测试,都测试全部数据,浪费时间,可以只测试那些特殊数据即可(切记).
- 为AM335x移植Linux内核主线代码(35)使用platform中的GPIO
http://www.eefocus.com/marianna/blog/15-02/310352_46e8f.html 使用GPIO,当然可以自己编写驱动,比如之前的第34节,也可以使用Kernel ...
- Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)
0 Homebrew是啥? “Homebrew installs the stuff you need that Apple didn’t.——Homebrew 使 OS X 更完整”. Homebr ...
- python基础整理笔记(四)
一. python 打开文件的方法 1. python中使用open函数打开文件,需要设定的参数包括文件的路径和打开的模式.示例如下: f = open('a.txt', 'r+') 2. f为打开文 ...
- 让谷歌浏览器 chrome 支持小于12px的字体
webkit的私有属性:{-webkit-text-size-adjust:none;} 但是,在最新版的谷歌里.已经不在支持这个属性啦. 用css3的transform:scale()缩放大小,但是 ...
- [2013 Final] Colors
Description Ziyao has a big drawing board with N * M squares. At the beginning, all the squares on t ...
- log4net简单配置内容
首先将log4net.dll下载来,添加到项目引用中: 在assembly文件最后面加(其实没关系的): [assembly: log4net.Config.XmlConfigurator(Confi ...