Swift 计算字符串展示的区域
一、如果是普通文本,那么可以采用NSString的方法,代码如下:
import UIKit
import PlaygroundSupport class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white let label = UILabel()
label.text = "永和九年,岁在癸丑,暮春之初,会于会稽山阴之兰亭,修稧(禊)事也。群贤毕至,少长咸集。此地有崇山峻领(岭),茂林修竹;又有清流激湍,映带左右,引以为流觞曲水,列坐其次。虽无丝竹管弦之盛,一觞一咏,亦足以畅叙幽情。是日也,天朗气清,惠风和畅。仰观宇宙之大,俯察品类之盛。所以游目骋怀,足以极视听之娱,信可乐也。"
label.textColor = .black
label.layer.borderColor = UIColor.red.cgColor
label.layer.borderWidth = 1.0
let font = UIFont.systemFont(ofSize: 16.0)
label.font = font let size = (label.text! as NSString).boundingRect(with: CGSize(width: 228, height: 1000), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : font], context: nil)
print(size)
label.frame = CGRect(x: 50, y: 250, width: size.size.width, height: size.size.height)
label.numberOfLines = 0
view.addSubview(label)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
https://developer.apple.com/documentation/foundation/nsstring/1524729-boundingrectwithsize
如果是富文本, 同样是类似的方法,https://developer.apple.com/documentation/foundation/nsattributedstring/1529154-boundingrectwithsize/
- (CGRect)boundingRectWithSize:(CGSize)size
options:(NSStringDrawingOptions)options
context:(NSStringDrawingContext *)context;
但是,文档中提示,返回的bounds是浮点数,需要采用向上取整的方式,避免采用这个尺寸后文字容纳不下
Discussion
You can use this method to compute the space required to draw the string. The constraints you specify in the size parameter are a guide for the renderer for how to size the string. However, the actual bounding rectangle returned by this method can be larger than the constraints if additional space is needed to render the entire string. Typically, the renderer preserves the width constraint and adjusts the height constraint as needed.
In iOS 7 and later, this method returns fractional sizes (in the size component of the returned rectangle); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
Swift 计算字符串展示的区域的更多相关文章
- [Swift通天遁地]五、高级扩展-(14)扩展String快速计算字符串中的各种数学表达式
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- iOS - Swift String 字符串
前言 public struct String public class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCodin ...
- NET MVC全局异常处理(一) 【转载】网站遭遇DDoS攻击怎么办 使用 HttpRequester 更方便的发起 HTTP 请求 C#文件流。 Url的Base64编码以及解码 C#计算字符串长度,汉字算两个字符 2019周笔记(2.18-2.23) Mysql语句中当前时间不能直接使用C#中的Date.Now传输 Mysql中Count函数的正确使用
NET MVC全局异常处理(一) 目录 .NET MVC全局异常处理 IIS配置 静态错误页配置 .NET错误页配置 程序设置 全局异常配置 .NET MVC全局异常处理 一直知道有.NET有相关 ...
- 窥探Swift之字符串(String)
之前总结过Objective-C中的字符串<Objective-C精选字符串处理方法>,学习一门新语言怎么能少的了字符串呢.Swift中的String和Objective-C语言中NSSt ...
- Swift 常用字符串操作
原文链接:http://www.jianshu.com/p/52e7580166ff 版本2:增加了Swift 2.0的语法,与Swift 1.2的语法相比,主要是:advance方法变成了advan ...
- java算法(1)---余弦相似度计算字符串相似率
余弦相似度计算字符串相似率 功能需求:最近在做通过爬虫技术去爬取各大相关网站的新闻,储存到公司数据中.这里面就有一个技术点,就是如何保证你已爬取的新闻,再有相似的新闻 或者一样的新闻,那就不存储到数据 ...
- 地图 SDK 系列教程-在地图上展示指定区域(转载)
腾讯位置服务地图SDK是一套提供多种地理位置服务的应用程序接口.通过调用该接口,开发者可以在自己的应用中加入地图相关的功能(如地图展示.标注.绘制图形等),轻松访问腾讯地图服务和数据,构建功能丰富.交 ...
- php计算字符串长度
/** * 计算字符串的长度(非字节) * 先用正则将字符串分解为个体单元,然后再计算单元的个数即得出字符串的长度 * from wordpress * @param string $string * ...
- [转载] python 计算字符串长度
本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...
- C# 计算字符串在控制台中的显示长度
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
随机推荐
- k8s集群部署1.28.2版本(无坑)
Kubernetes-1.28.2 集群介绍及搭建 一.Kubernetes 概述 1.什么是Kubernetes? K8S 的全称为 Kubernetes.用于自动部署.扩展和管理"容器化 ...
- 【Oracle】year must be between -4713 and +9999,and not be 0
[Oracle]year must be between -4713 and +9999,and not be 0 year must be between -4713 and +9999,and n ...
- 国庆集训 Day1 复盘笔记
9.25 \({\color{Green} \mathrm{A\ -\ Powered\ Addition}}\) 只要把序列扫一遍,然后求出目前最大值与当前值的差的最大值 \(x\),再 \(log ...
- 入门即享受!coolbpf 硬核提升 BPF 开发效率 | 龙蜥技术
简介: 干货必备!何为享受式开发? 编者按:BPF 技术还在如火如荼的发展着,本文先通过对 BPF 知识的介绍,带领大家入门 BPF,然后介绍 coolbpf 的远程编译(原名 LCC,LibbpfC ...
- [Tools] Kali Linux 高清屏扩大系统字体、BurpSuite、OpenVAS
系统检索 Setting Manager,Appearance -> Settings,选择 Window Scaling:2x Terminal fonts: linux terminal 快 ...
- WPF 编写一个测试 WM_TOUCH 触摸消息延迟的应用
我听说在 Win10 到 Win11 的系统版本左右,微软加上了一大波触摸性能优化,准确来说是 HID 性能优化.我想测试一下在这些系统下,采用从 Windows 消息接收到 WM_TOUCH 触摸消 ...
- dotnet 在 UOS 国产系统上使用 MonoDevelop 进行拖控件开发 GTK 应用
先从一个 Hello World 应用开始,试试和古老的 WinForms 一样的拖控件式开发 在创建完成一个 GTK# 2.0 应用之后,咱可以试试开始拖控件的开发,当然这个开发方式开发出来的应用界 ...
- kubenetes1.26中安装kubesphere3.4版本
一.安装前环境准备 # kubesphere官网:https://kubesphere.io/zh/docs/v3.4/introduction/what-is-kubesphere/ # 1.kub ...
- vue-cli快速搭建项目的几个文件(一)
===========app.vue文件============= <template> <div id="app"> <router ...
- Java设计模式-观察者模式-SpringBoot实现
观察者模式 项目:https://gitee.com/KakarottoChen/blog-code.git 的:JavaSpringListener 一.Java观察者模式 Java观察者模式是一种 ...