点击超出裁剪视图边界的注册。
SwiftUI 似乎会在另一个视图内以全宽和全高布局图像,以为其提供纵横填充的填充模式。这很好,但是如果我想向视图添加点击动作,即使被裁剪,点击也会在视图边界之外注册。我可能做错了,或者可能有另一种方式。除了这段代码,我还尝试使用矩形的 .clippedShape 和 .contentShape 。我如何将它带到剪辑框架外的点击不会在底层图像上注册的地方。要重现切换到剪辑并点击图像将占据剪辑视图框架之外的区域。
import SwiftUI
struct ImageContainer : View {
var body: some View {
Image(systemName: "cube")
.resizable()
.scaledToFill()
.clipped()
}
}
struct ContentView: View {
@State var isClipped = false
@State var count = 0
var body: some View {
VStack{
Toggle(isOn: $isClipped) {
Text("Clip It ")
.font(.title)
}.frame(width: 175)
ZStack{
if self.isClipped == true{
ImageContainer()
.frame(width: 200, height: 200)
.contentShape(Rectangle())
.clipped()
.background(Color.blue)
.onTapGesture {
print("tapped mainContainer\(self.count)\n")
self.count += 1
}
}else{
ImageContainer()
.frame(width: 200, …Run Code Online (Sandbox Code Playgroud) 有没有办法获得包含真正下降器的 UIFont 的所有字形?似乎使用 CTLineGetTypographicBounds 不准确,并且为每一行返回完全相同的下降值。我以为它会提供我需要的信息,但它没有。所以现在我想看看我是否可以从包含真正下降的字形构建一个字符集,除非有另一种方法。最终目标将能够查看一行文本是否低于基线。
let line = CTLineCreateWithAttributedString(NSAttributedString(string: s, attributes: attr))
//let's get the real descent test
var a : CGFloat = 0
var d : CGFloat = 0
var l : CGFloat = 0
let bounds = CTLineGetTypographicBounds(line, &a, &d, &l)
print("the descent is \(d)")
print("the ascent is \(a)")
print("the leading is \(l)")
Run Code Online (Sandbox Code Playgroud)