- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个具有动画属性的自定义标签,所以我决定使用 CATextLayer
而不是直接使用 CoreText
..
我想出了下面的代码(我用 playground 来测试东西):
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
public extension CGRect {
public static func centerRect(_ rectToCenter: CGRect, in rect: CGRect) -> CGRect {
return CGRect(x: rect.origin.x + ((rect.width - rectToCenter.width) / 2.0),
y: rect.origin.y + ((rect.height - rectToCenter.height) / 2.0),
width: rectToCenter.width,
height: rectToCenter.height)
}
}
class MyLabel : UIView {
private let textLayer = CATextLayer()
public var textColor: UIColor = UIColor.black
public var font: UIFont = UIFont.systemFont(ofSize: 17.0)
private var _lineBreak: NSLineBreakMode = .byTruncatingTail
init() {
super.init(frame: .zero)
self.text = nil
self.textAlignment = .natural
self.lineBreakMode = .byTruncatingTail
self.textLayer.isWrapped = true
self.layer.addSublayer(self.textLayer)
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//Update the CATextLayer's attributed string when this property is set..
public var text: String? {
//Getter just returns the CATextLayer's string
get {
if let attrString = self.textLayer.string as? NSAttributedString {
return attrString.string
}
return self.textLayer.string as? String
}
//Setter creates an attributed string with paragraph style..
//Font and colour..
set {
if let value = newValue {
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.alignment = self.textAlignment
paragraphStyle.lineBreakMode = self.lineBreakMode
self.textLayer.string = NSMutableAttributedString(string: value, attributes: [
.foregroundColor: self.textColor,
.font: self.font])
}
else {
self.textLayer.string = nil
}
}
}
//Convert NSTextAlignment to kCAAlignment String for CATextLayer
public var textAlignment: NSTextAlignment {
get {
switch self.textLayer.alignmentMode {
case kCAAlignmentLeft:
return .left
case kCAAlignmentCenter:
return .center
case kCAAlignmentRight:
return .right
case kCAAlignmentJustified:
return .justified
default:
return .natural
}
}
set {
switch newValue {
case .left:
self.textLayer.alignmentMode = kCAAlignmentLeft
case .center:
self.textLayer.alignmentMode = kCAAlignmentCenter
case .right:
self.textLayer.alignmentMode = kCAAlignmentRight
case .justified:
self.textLayer.alignmentMode = kCAAlignmentJustified
default:
self.textLayer.alignmentMode = kCAAlignmentNatural
}
}
}
//Convert NSLineBreakMode to kCAAlignmentMode String
public var lineBreakMode: NSLineBreakMode {
get {
return _lineBreak
}
set {
_lineBreak = newValue
switch newValue {
case .byWordWrapping:
self.textLayer.isWrapped = true
self.textLayer.truncationMode = kCATruncationNone
case .byCharWrapping:
self.textLayer.isWrapped = true
self.textLayer.truncationMode = kCATruncationNone
case .byClipping:
self.textLayer.isWrapped = false
self.textLayer.truncationMode = kCATruncationNone
case .byTruncatingHead:
self.textLayer.truncationMode = kCATruncationStart
case .byTruncatingMiddle:
self.textLayer.truncationMode = kCATruncationMiddle
case .byTruncatingTail:
self.textLayer.truncationMode = kCATruncationEnd
}
}
}
//Override layoutSubviews to render the CATextLayer..
internal override func layoutSubviews() {
super.layoutSubviews()
//Calculate attributed string size..
let string = self.textLayer.string as! NSAttributedString
var rect = string.boundingRect(with: self.bounds.size, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
//If you change dy to 1.0, Text will render weirdly!
rect = rect.insetBy(dx: 0.0, dy: 0.0)
//Render the textLayer by centering it in its parent..
self.textLayer.contentsScale = UIScreen.main.scale
self.textLayer.rasterizationScale = UIScreen.main.scale
self.textLayer.frame = CGRect.centerRect(rect, in: self.bounds)
self.textLayer.backgroundColor = UIColor.lightGray.cgColor
}
//Somehow always returns 21.. ={
//Not used right now because it doesn't work.. at all..
private func sizeOfTextThatFits(size: CGSize) -> CGSize {
if let string = self.textLayer.string as? NSAttributedString {
let path = CGMutablePath()
path.addRect(CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
let frameSetter = CTFramesetterCreateWithAttributedString(string as CFAttributedString)
let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
let lines = CTFrameGetLines(frame) as NSArray
var lineWidth: CGFloat = 0.0
var yOffset: CGFloat = 0.0
for line in lines {
let ctLine = line as! CTLine
var ascent: CGFloat = 0.0
var descent: CGFloat = 0.0
var leading: CGFloat = 0.0
lineWidth = CGFloat(max(CTLineGetTypographicBounds(ctLine, &ascent, &descent, &leading), Double(lineWidth)))
yOffset += ascent + descent + leading;
}
return CGSize(width: lineWidth, height: yOffset)
}
return .zero
}
}
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = MyLabel()
label.backgroundColor = UIColor.red
label.frame = CGRect(x: 150, y: 200, width: 200, height: 200)
label.text = "Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
问题是,如果我使用 NSParagraphStyle
,它根本不会正确渲染或计算大小!
如果我删除段落样式,它可以很好地呈现,但从不遵守换行模式,并且大小不正确..
知道我做错了什么吗?我怎样才能让它服从段落样式的换行模式?为什么在应用段落样式时它总是计算大小为 21?
当删除段落样式但在 CATextLayer
上设置 TruncateEnd
时(它永远不会截断并且最后一行与倒数第二行之间的间距更大):
最佳答案
我发现结束省略号字形不是从属性字符串中获取它的大小,而是从 CATextLayer.fontSize
属性中获取它的大小。
这似乎默认为一个很大的值,这解释了您的文本在您的第一个示例中被向下移动和剪裁。向下扩展文本层的框架大小,设置CATextLayer
的isWrapped=false
,您将看到您的文本加上一个巨大的...省略号字形!
设置 CATextLayer.fontSize
以匹配属性文本的字体大小可以解决此问题,但它看起来像是一个 Apple 错误。
我也无法通过单独设置属性字符串属性来使其工作..
关于ios - CATextLayer 渲染具有截断和段落样式的 AttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48482657/
我有一个 1850-2005 年月地表气温的 netCDF 文件。如何在 unix 中截断文件,以便新文件的时间维度从 1855 年到 2005 年?反之亦然,截断文件,使其改为 1850-2000?
我收到此错误 Bulk load data conversion error (truncation) for row 1, column 12 (is_download) 这是 csv...它只有一
我有一个列表,它是电子邮件正文,每行末尾都有一个日期和时间戳。邮票的格式是一致的,所以可以从右边计算表达式。以下是示例数据: Dear Volunteer2018-05-21 19:59:15 You
我正在使用内置 truncatewords_html Django 的过滤器,它在最后添加了“...”,相反,我想用“查看更多”链接替换它。 我怎样才能做到这一点? 最佳答案 最好编写自己的过滤器。您
我正在使用 SQL 加载器将我的数据加载到数据库中。 在插入数据之前,我需要删除表中的现有数据: options(skip=1,load=250000,errors=0,ROWS=30000,BIND
我正在尝试掌握消息队列的窍门。由于某种原因,当显示我在控制台中输入的消息时,字符串有时会被截断或更改。有谁知道为什么会发生这种情况? void *readFromQueue() { int r
我正在使用mediawiki API(例如http://en.wikipedia.org/w/api.php),我希望能够“截断”mysql表以便重置本地安装,同时保留一些表(用户,?...) 。SQ
想要截断一个存在的表: IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'mytable') TRUNCATE
我正在实现一个使用 Python Numpy 包的程序。我正在尝试修改数组的元素,以便我只需采用 elem[i][j] 并将其设置为 elem[i][j]/10。但是,我不断收到某种截断,其中元素在操
我有一个 64 位 long int,其中包含一些位域。我需要将存储在第二个和第三个字节中的 16 位带符号整数添加到一个 32 位值中。我正在使用这样的东西: u32 Function( s32 v
我有这样的文字:“我的文字是 blabla blabla, lala lala”。 我希望在我的 UILabel 中有这样的文本:“My text is ...lala”。 如何配置我的 UILabe
Umbraco Truncate 似乎不适合我,当我使用正确的代码(根据互联网)时,它会不断出错。我不知道它有什么问题。 错误代码: Compiler Error Message: CS1502: T
早些时候,我是使用 JS 动态地完成它的。但是我们遇到了一些性能问题,因为我们必须提供一个替代选项。 我现在使用文本溢出样式截断选项卡名称上的长文本。 但我有一个小问题,如果有人能解决的话 目前这是我
使用Rspec时截断、事务和删除数据库策略有什么区别?我找不到任何资源来解释这一点。我阅读了 Database Cleaner 自述文件,但它没有解释它们各自的作用。 为什么我们必须对 capybar
当然, 尚有诸位前辈也曾把以上三种方案结合一二, 以达到更广泛的适应度. 不过, 这厢先前在网路上搜索许久, 却未曾寻到三种方案合为一体的尝试, 于是只好自己动手写一下了:) 没有dem
有没有办法截断HSQLDB中的所有表? 如果这不可能,是否有任何方法可以级联删除具有外键引用的表? 最佳答案 可以截断模式中的所有表: 截断模式并提交 此命令有一些选项在指南中有描述: http://
我有一个要截断的时间戳。我正在使用 trunc oracle中的函数。这似乎做了我想要的但是从文档中它应该只接受日期而不是时间戳 select TRUNC(TO_DATE('22-AUG-13'),
我读到一旦你耗尽了一个节点,你就可以删除文件然后重新启动。它工作正常, 但我只是通过排空所有节点,关闭整个集群,删除文件并重新启动来尝试它。 如果我一次只重启一个节点会怎样?据我了解有风险 重新启动的
我想截断 d3 中超过预定义限制的文本。 我不知道该怎么做。 这是我现在所拥有的: node.append("text") .attr("dx", 20) .attr("dy", ".20
嗨 Guyz 我有一个固定宽度的 WPF TextBlock 说 100 ,如果字符串不适合宽度,则最后一个字符总是被截断,因为所有字符的大小都不相同。我不想剪切字符而是我想从那里跳过文本并只显示没有
我是一名优秀的程序员,十分优秀!