Code Monkey home page Code Monkey logo

attributedtext's Introduction

Warning

This repo has been archived.

From macOS 12+ / iOS 15+ / tvOS 15+ / watchOS 8+, you can use AttributedString with the SwiftUI Text view.

AttributedText

Twitter: @gonzalezreal

AttributedText is a Swift µpackage that provides NSAttributedString rendering in SwiftUI by wrapping either an NSTextView or a UITextView depending on the platform.

Supported Platforms

  • macOS 11.0+
  • iOS 14.0+
  • tvOS 14.0+

Usage

import AttributedText
import SwiftUI

struct ContentView: View {
  var body: some View {
    AttributedText {
      let result = NSMutableAttributedString(
        string: """
          After the Big Bang
          A brief summary of time
          Life on earth
          10 billion years
          You reading this
          13.7 billion years
          """
      )

      result.addAttributes(
        [.font: UIFont.preferredFont(forTextStyle: .title1)],
        range: NSRange(location: 0, length: 18)
      )
      result.addAttributes(
        [.link: URL(string: "https://en.wikipedia.org/wiki/Big_Bang")!],
        range: NSRange(location: 10, length: 8)
      )
      result.addAttributes(
        [.font: UIFont.preferredFont(forTextStyle: .body)],
        range: NSRange(location: 19, length: 23)
      )
      result.addAttributes(
        [.font: UIFont.preferredFont(forTextStyle: .title2)],
        range: NSRange(location: 43, length: 13)
      )
      result.addAttributes(
        [.font: UIFont.preferredFont(forTextStyle: .body)],
        range: NSRange(location: 57, length: 16)
      )
      result.addAttributes(
        [.font: UIFont.preferredFont(forTextStyle: .title2)],
        range: NSRange(location: 74, length: 16))
      result.addAttributes(
        [.font: UIFont.preferredFont(forTextStyle: .body)],
        range: NSRange(location: 91, length: 18)
      )

      return result
    }
    .background(Color.gray.opacity(0.5))
    .accentColor(.purple)
  }
}

iOSScreenshot

An AttributedText view takes all the available width and adjusts its height to fit the contents.

To change the text alignment or line break mode, you need to add a .paragraphStyle attribute to the attributed string.

Installation

You can add AttributedText to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter https://github.com/gonzalezreal/AttributedText into the package repository URL text field
  3. Link AttributedText to your application target

attributedtext's People

Contributors

gonzalezreal avatar kaylanx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

attributedtext's Issues

Not work Dynamic Size for Font

Good afternoon!
Thanks for your library, it helped a lot in development. But I noticed that if you change the font size in the application, AttibutedText will not change size, to fix this you need to add this line:

textView.adjustsFontForContentSizeCategory = true

Runtime Issue. Publishing changes from within view updates is not allowed. Xcode 14.0 beta 5

SwiftUI Runtime Issue: Publishing changes from within view updates is not allowed, this will cause undefined behavior.

Xcode version: 14.0 beta 5

Screen Shot 2022-08-20 at 1 54 27 PM

Can be reproduced by using this code:

import SwiftUI
import AttributedText


struct ContentView: View {
    var body: some View {
        AttributedText {
            NSAttributedString(string: "Hello world")
        }
    }
}

P.S. This issue also effects MarkdownUI

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.