我正在嘗試調整長度變化很大的文本元素的字體大小,以便它填充它所在的整個框架并且不需要截斷任何單詞(在 iOS 應用程式中)。在下面的螢屏截圖中,我希望句子到達螢屏底部的按鈕。
請在下面找到我的代碼結構。我已經看到一些關于 SO 處理這個問題的執行緒,但由于我在 Swift 方面的技能很差,我無法復制解決方案......如果有人好心給我詳細解釋一下哪些行應該插入代碼以及放置它們的位置,我將非常感激!
我試圖手動調整字體大小,通過根據 Text 作為引數接收的字串的長度通過函式遞增或遞減它,但它只會產生一團糟,因為長詞需要自己的單獨行,這破壞了我的計算。
帶著感謝,
朱利安
import SwiftUI
import AVKit
import UIKit
import AVFoundation
import Foundation
import CoreServices
import CoreData
import MediaPlayer
struct ContentView: View {
// Various functions
@State var audioPlayer: AVAudioPlayer!
var body: some View {
VStack {
var currentSentence = "string of variable length, could be a long or a very short sentence"
Text(currentSentence) // the variable currentSentence contains a string whose length varies greatly
.font(.system(size: 40, weight: .light, design: .serif))
HStack {
Spacer()
// Play button
Button(action: {
// Action
}) {
Image(systemName: "play.square.fill").resizable()
.frame(width: 150.0, height: 200.0, alignment: .bottom)
}
.frame(maxHeight: .infinity, alignment: .bottom)
Spacer()
// Pause button
Button(action: {
// Action
}){
Image(systemName: "square.slash").resizable()
.frame(width: 150.0, height: 200.0)
.aspectRatio(contentMode: .fill)
}
.frame(maxHeight: .infinity, alignment: .bottom)
Spacer()
}
}
.frame(
minWidth: 0,
maxWidth: .infinity,
minHeight: 0,
maxHeight: .infinity,
alignment: .topLeading
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

uj5u.com熱心網友回復:
您需要在文本上使用 .minimumScaleFactor 并將文本字體設定為最大。
struct ContentView: View {
var body: some View {
VStack (spacing: 0) {
var currentSentence = "string of variable length, could be a long or a very short sentence"
Text(currentSentence)
.font(.system(size: 100, weight: .light, design: .serif))
.minimumScaleFactor(0.1)
Spacer()
HStack (alignment: .bottom, spacing: 0) {
Spacer()
// Play button
Button(action: {
// Action
}) {
Image(systemName: "play.square.fill").resizable()
.frame(width: 150.0, height: 200.0, alignment: .bottom)
}
Spacer()
// Pause button
Button(action: {
// Action
}){
Image(systemName: "square.slash").resizable()
.frame(width: 150.0, height: 200.0)
.aspectRatio(contentMode: .fill)
}
Spacer()
}
}
.edgesIgnoringSafeArea([.bottom])
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
}
}
}
在某些情況下,文本和按鈕之間可能會有一點空間,這意味著更大字體的新行無法容納在那里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/405827.html
標籤:
上一篇:如何將資料發送到以下JSON資料的post請求引數中?
下一篇:關聯陣列鍵數
