在 iOS15 中运行以下代码时,内容中的 URL 或电子邮件地址的颜色使用强调色而不是前景色。我怎样才能覆盖它?
Text("Send a message to john@email.com to request support")
.foregroundColor(.blue)
Run Code Online (Sandbox Code Playgroud)
有没有办法改变Textusing的输出格式init(_ date: Date, style: Text.DateStyle)?
使用 a .timer,输出类似于:0:42,但我想要类似00:00:42.
背景
我想创建一个运行计时器的小部件(iOS 14),并且我认为每秒触发小部件更新不是一个好主意,这甚至可能无法可靠地工作,至少这不是小部件的缩进方式要使用的。所以我考虑使用这个预定义的计时器功能Text。
我对 SwiftUI 还很陌生,还不太了解所有功能。那么也许我可以自己创建一个类似的自定义文本视图?关于如何实现这一目标有什么想法吗?
或者换个方式问:有没有办法自己创建这样的自我更新组件,并且也可以在 iOS 14 小部件中工作?(似乎使用Timer.publish更新视图在 iOS 14 小部件中不起作用)
I'm facing the following problem with SwiftUI Text: In the following example SwiftUI breaks the word "Amazement" into "amazeme" on the first line and "nt" on the second. How to avoid it, isn't it a bug?
I want the word "amazement" to be written on one line. Is there any modifier that can allow this (don't divide words or something)?
Tried .allowsTightening, .fixedSize. Changed the order of modifiers, Didn't help.
Is it a bug or we currently don't have an …
我需要精确控制文本所占据的区域。我创建了最基本的程序,显示意外的顶部和底部间距被添加到文本中。这些额外的填充从哪里来?如何摆脱它?
@main
struct myApp: App {
init() { }
var body: some Scene {
WindowGroup {
Text("80")
.font(.system(size: 30, weight: .bold, design: .default))
.background(Color.red)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个带有动态文本的按钮。我希望缩小文本以适应它(尽可能多),并将文本换行到一定数量的行(可能是 2 行?)。
默认情况下,SwiftUI 似乎将文本包裹在单词上。但似乎当我在视图scaledToFit上使用修饰符时Text,它会阻止单词换行。
这是一些游乐场代码来说明:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
let lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint …Run Code Online (Sandbox Code Playgroud) 目标是拥有如下所示的东西:
我知道.rotationEffect()以及此处提供的解决方案。
然而,这个解决方案的问题是它不旋转框架,它只旋转文本本身。
下图来自 Xcode 中的 Canvas 预览及其下面的代码。
HStack(spacing: 10) {
Text("Distance")
.rotationEffect(.degrees(270))
.foregroundColor(.black)
.minimumScaleFactor(0.01)
Rectangle()
.foregroundColor(.black)
.frame(width: 3)
.padding([.top, .bottom])
Spacer()
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,文本超出了框架,框架保持其原始宽度,这使得文本很难紧贴视图的一侧,也很难让垂直线紧贴文本的一侧。
我能够弄清楚如何获得所需结果的唯一方法是使用修饰符.offset(x:)。但这感觉很混乱,并且我认为可能会导致错误。
有什么办法可以让框架随着文字一起旋转吗?
您好,我使用 swiftUI 创建此设计,但我不明白为什么 Text() 会向字符串值添加更多填充,我只想将其添加到前导尾随,例如边距
结果:
代码:
struct ProductDetailSwiftUIView: View {
var body: some View {
ZStack {
ScrollView {
VStack {
Rectangle()
.frame(height: 213)
.foregroundColor(Color.blue)
Text("Fresas Congeladas La Huerta 500g")
.foregroundColor(.black)
.font(Font.bodySemibold(22))
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity)
.background(Color.red)
.padding(.leading, 16)
.padding(.trailing, 16)
}
}.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
print("Hello world")
} label: {
Label("Back", image: "cartLine")
}.overlay(Badge(count: 1))
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想在一个Text视图中使用多种文本格式SwiftUI中使用多种文本格式。这看起来像这样:
\n\n我想在同一个文本框中同时拥有多种文本格式,即使它真的真的真的真的
\n真的文本较长,因此需要正确换行。(甚至可能包括链接!)
我发现了这个(如何在 TextField swiftUI 中加粗文本?) \xc2\xa0\xc2\xa0thread ,但这根本不是我想要的。
\n这将类似于HTML\ 的工作span
我有一个经常使用的自定义字体修饰符,但是它使代码很长,因为我需要将其添加到多个位置。
这是修改器:
.font(.custom("Bebas Neue", size: 24)).foregroundStyle(LinearGradient(gradient: Gradient(colors: [Color("lb"), Color("rb")]),startPoint: .top, endPoint: .bottom))
Run Code Online (Sandbox Code Playgroud)
我怎样才能缩短它,以便我可以用一个词左右完美地导入它?
swiftui ×10
swiftui-text ×10
swift ×5
ios ×2
ios14 ×2
text ×2
xcode ×2
ios15 ×1
swiftui-view ×1
textview ×1