Swift iOS 使字体大小适应框架
Swift iOS adapt font size to frame
我正在尝试调整长度变化很大的文本元素的字体大小,以便它填满它所在的整个框架,并且不需要截断任何单词(在 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()
}
}
您需要在文字上使用.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()
}
}
}
在某些情况下,文本和按钮之间可能会有一点 space,这意味着字体较大的新行放不下。
我正在尝试调整长度变化很大的文本元素的字体大小,以便它填满它所在的整个框架,并且不需要截断任何单词(在 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()
}
}
您需要在文字上使用.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()
}
}
}
在某些情况下,文本和按钮之间可能会有一点 space,这意味着字体较大的新行放不下。