如何在 SwiftUI 中使用属性字符串

Dat*_*kar 22 swift swiftui

如何在 SwiftUI 中使用 AttributedString。没有可用于在文本中使用 AttributedString 的 API

ahe*_*eze 38

iOS 15

我们终于得到了AttributedString!它真的很容易使用。

Multiple Attributed Strings with different attributes

struct ContentView: View {
    var body: some View {
        VStack(spacing: 40) {
            
            /// Note: You can replace `$0` with `string in string`
            
            VStack {
                Text("Regular")
                Text("Italics") { $0.font = Font.system(size: 17).italic() }
                Text("Bold") { $0.font = Font.system(size: 17).bold() }
                Text("Strikethrough") { $0.strikethroughStyle = Text.LineStyle(pattern: .solid, color: .red) }
                Text("Code") { $0.font = Font.system(size: 17, design: .monospaced) }
                Text("Foreground Color") { $0.foregroundColor = Color.purple }
                Text("Background Color") { $0.backgroundColor = Color.yellow }
                Text("Underline") { $0.underlineColor = Color.green }
            }
            
            VStack {
                Text("Kern") { $0.kern = CGFloat(10) }
                Text("Tracking") { $0.tracking = CGFloat(10) }
            }
            
            VStack {
                Text("Baseline Offset") { $0.baselineOffset = CGFloat(10) }
                Text("Link") { $0.link = URL(string: "https://apple.com") }
            }
        }
    }
}

/// extension to make applying AttributedString even easier
extension Text {
    init(_ string: String, configure: ((inout AttributedString) -> Void)) {
        var attributedString = AttributedString(string) /// create an `AttributedString`
        configure(&attributedString) /// configure using the closure
        self.init(attributedString) /// initialize a `Text`
    }
}
Run Code Online (Sandbox Code Playgroud)

要将属性应用于特定范围,请使用range(of:options:locale:)方法。

Attributed String with different colors

struct ContentView: View {
    var body: some View {
        Text("Some Attributed String") { string in
            string.foregroundColor = .blue
            if let range = string.range(of: "Attributed") { /// here!
                string[range].foregroundColor = .red
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅我的文章了解更多详情。另外,您可以使用 Markdown


Asp*_*eri 33

的想法attributed string带有属性的字符串。在 SwiftUI 中,这个想法是通过Text属性修饰符和+运算符实现的。就像下面的例子:

带有属性的 SwiftUI 文本

Group {
    Text("Bold")
        .fontWeight(.bold) +
    Text("Underlined")
        .underline() +
    Text("Color")
        .foregroundColor(Color.red)
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您支持多种语言,这绝对不好。 (6认同)
  • 在这里,如果我们像这样使用,并且如果我们想添加单击颜色或任何其他文本,我们不能 (3认同)

Moj*_*ini 32

iOS 15 和 Swift 5.5

Text 现在支持降价,您还可以创建自定义属性:

在此处输入图片说明

您甚至可以远程获取定义的属性,例如:

在此处输入图片说明


iOS 13 和 14

您可以将多个Text对象与一个简单的+运算符组合在一起,这将处理一些属性:

在此处输入图片说明

每个都可以有多个特定的修饰符


完全支持的回退!

由于它不直接支持Text(直到 iOS 15),您可以将其带到UILabel那里并以任何您喜欢的方式修改它:

执行:

struct UIKLabel: UIViewRepresentable {

    typealias TheUIView = UILabel
    fileprivate var configuration = { (view: TheUIView) in }

    func makeUIView(context: UIViewRepresentableContext<Self>) -> TheUIView { TheUIView() }
    func updateUIView(_ uiView: TheUIView, context: UIViewRepresentableContext<Self>) {
        configuration(uiView)
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

var body: some View {
    UIKLabel {
        $0.attributedText = NSAttributedString(string: "HelloWorld")
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法使其中一个文本可点击?tapGesture 似乎不合适,因为它返回“某些视图”而不是“文本”? (3认同)
  • 用法:“传递给不带参数的调用的参数” (2认同)
  • @MojtabaHosseini - 如何在“UIKLabel”上启用对齐?已尝试在堆栈上进行包装并查看自己的视图似乎没有任何效果。 (2认同)

小智 13

如果你想用 NSAttributedString 实现动态高度文本,你可以使用这个:

执行:

 struct TextWithAttributedString: View {

    var attributedText: NSAttributedString
    @State private var height: CGFloat = .zero

    var body: some View {
        InternalTextView(attributedText: attributedText, dynamicHeight: $height)
            .frame(minHeight: height)
    }

    struct InternalTextView: UIViewRepresentable {

        var attributedText: NSAttributedString
        @Binding var dynamicHeight: CGFloat

        func makeUIView(context: Context) -> UITextView {
            let textView = UITextView()
            textView.textAlignment = .justified
            textView.isScrollEnabled = false
            textView.isUserInteractionEnabled = false
            textView.showsVerticalScrollIndicator = false
            textView.showsHorizontalScrollIndicator = false
            textView.allowsEditingTextAttributes = false
            textView.backgroundColor = .clear
            textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
            textView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
            return textView
        }

        func updateUIView(_ uiView: UITextView, context: Context) {
            uiView.attributedText = attributedText
            DispatchQueue.main.async {
                dynamicHeight = uiView.sizeThatFits(CGSize(width: uiView.bounds.width, height: CGFloat.greatestFiniteMagnitude)).height
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

    VStack {
       TextWithAttributedString(attributedText: viewModel.description)
         .padding([.leading, .trailing], self.horizontalPadding)
         .layoutPriority(1)
         .background(Color.clear)
    }
    .transition(.opacity)
    .animation(.linear)
Run Code Online (Sandbox Code Playgroud)


aes*_*eis 7

对此有很多答案都使用UILabelor UITextView。我很好奇是否有可能创建一个不依赖任何 UIKit 功能的原生 SwiftUI 实现。这代表了一个适合我当前需求的实现。它与NSAttributedString规范的完整实现相去甚远,但它绝对足以满足最基本的需求。NSAttributedString接受 HTML 字符串的构造函数是我制作的自定义类别,非常容易实现。如果有人想用它运行并创建一个更健壮和完整的组件,你就是我的英雄。可惜我没有时间做这样的项目。

//
//  AttributedText.swift
//
import SwiftUI

struct AttributedTextBlock {
    let content: String
    let font: Font?
    let color: Color?
}

struct AttributedText: View {
    var attributedText: NSAttributedString?
    
    private var descriptions: [AttributedTextBlock] = []
    
    init(_ attributedText: NSAttributedString?) {
        self.attributedText = attributedText
        
        self.extractDescriptions()
    }
    
    init(stringKey: String) {
        self.init(NSAttributedString(htmlString: NSLocalizedString(stringKey, comment: "")))
    }
    
    init(htmlString: String) {
        self.init(NSAttributedString(htmlString: htmlString))
    }
    
    private mutating func extractDescriptions()  {
        if let text = attributedText {
            text.enumerateAttributes(in: NSMakeRange(0, text.length), options: [], using: { (attribute, range, stop) in
                let substring = (text.string as NSString).substring(with: range)
                let font =  (attribute[.font] as? UIFont).map { Font.custom($0.fontName, size: $0.pointSize) }
                let color = (attribute[.foregroundColor] as? UIColor).map { Color($0) }
                descriptions.append(AttributedTextBlock(content: substring,
                                                        font: font,
                                                        color: color))
            })
        }
    }
    
    var body: some View {
        descriptions.map { description in
            Text(description.content)
                .font(description.font)
                .foregroundColor(description.color)
        }.reduce(Text("")) { (result, text) in
            result + text
        }
    }
}

struct AttributedText_Previews: PreviewProvider {
    static var previews: some View {
        AttributedText(htmlString: "Hello! <b>World</b>")
    }
}
Run Code Online (Sandbox Code Playgroud)