µframework for Attributed strings.

Overview

Attributed

Build Status CodeCov Swift 5.0 CocoaPods compatible Carthage compatible License

µframework for Attributed strings.

What is Attributed?

Attributed aims to be a drop in replacement to the current version of the NSAttributedString API.

The NSAttributedString interface has a few shortcomings. If you donʼt know the key and type of value needed to set a certain attribute, you have to spend time checking documentation. Another concern is safety: passing a dictionary of type [String: Any] to the constructor of NSAttributedString is a potential crash at runtime waiting to happen.

Attributed provides developers a nicer alternative by extending the current NSAttributedString interface with a fluent, strongly typed, and easy to use API.

Features

  • Create NSAttributedString instances with a strongly typed, simple, and fluid interface
  • Combine NSAttributedStrings with +
  • Partially apply Attributes to parts of an NSAttributedString by providing a Range

Donʼt see a feature you need?

Feel free to open an issue requesting the feature you want or send over a pull request!

Usage

Creating a new NSAttributedString by closure composition

"This is not a string".at.attributed {
  return $0.foreground(color: .red)
           .font(UIFont(name: "Chalkduster", size: 24.0)!)
           .underlineStyle(.styleSingle)
}

Creating a new NSAttributedString by passing an attributes object

First create an Attributes object:

let attributes = Attributes {
    return $0.foreground(color: .red)
             .font(UIFont(name: "Chalkduster", size: 24.0)!)
             .underlineStyle(.styleSingle)
}

then simply apply the Attributes to a String:

"Hello".at.attributed(with: attributes)

Combining NSAttributedString with +

This library defines an concatenation operator + for concatentating instances of NSAttributedString. + works with NSAttributedString no different than it does for String. This can be useful for combining NSAttributedStrings with different attributes to produce the desired effect without having to specify ranges to apply different attributes to.

let bodyAttributes = Attributes { 
    return $0.foreground(color: .purple)
             .font(UIFont(name: "Noteworthy-Light", size: 20.0)!)
}

let authorAttributes = bodyAttributes.foreground(color: .black)

"I think theres something strangely musical about noise.".at.attributed(with: bodyAttributes)
+ "\n  - Trent Reznor".at.attributed(with: authorAttributes)

Installation

Carthage

If you use Carthage to manage your dependencies, simply add Attributed to your Cartfile:

github "Nirma/Attributed"

If you use Carthage to build your dependencies, make sure you have added Attributed.framework to the "Linked Frameworks and Libraries" section of your target, and have included Attributed.framework in your Carthage framework copying build phase.

CocoaPods

If you use CocoaPods to manage your dependencies, simply add Attributed to your Podfile:

pod 'AttributedLib'

Requirements

  • Xcode 9.0
  • Swift 4.0+

Contribution

Contributions are more than welcome!

License

Attributed is free software, and may be redistributed under the terms specified in the LICENSE file.

Comments
  • I also can not fetch latest release version of this repo.

    I also can not fetch latest release version of this repo.

    My pod search AttributedLib result:

    -> AttributedLib (2.1.1)
       Modern Swift µframework for attributed strings.
       pod 'AttributedLib', '~> 2.1.1'
       - Homepage: https://github.com/Nirma/Attributed
       - Source:   https://github.com/Nirma/Attributed.git
       - Versions: 2.1.1, 2.1.0, 2.0.2, 2.0.1, 2.0.0, 1.3.0, 1.2.0, 1.1.0, 1.0.0,
       0.2.2, 0.2.1, 0.2.0, 0.1.1, 0.1.0 [master repo]
    

    I think problem is in here:

    s.source           = { :git => 'https://github.com/Nirma/Attributed.git', :tag => s.version.to_s }
    

    I also check your tag position: image

    So I consider maybe you should move the tag at the correct place.

    opened by Arcovv 6
  • Strikethrough attribute is no longer working?

    Strikethrough attribute is no longer working?

    Uses of strikthrough attribute like this is no longer working.

       attributes.append("Siddarth".at.attributed({
                            $0.strikeThroughStyle(.styleSingle)
                                .lineBreakMode(.byWordWrapping)
                        }))
    
    
    opened by kidsid-Ixigo 5
  • set internal dictionary property Public

    set internal dictionary property Public

    in some scenarios I need to access internal dictionary. for example:

    let attributes = Attributes {
                return $0.foreground(color: .white)
                .alignment(.center)
                .font(UIFont.DinCondensedBold(of: 17))
            }
            
            let icon = String(value)
            // Get the width and height that the text will occupy.
            let iconSize = icon.size(attributes: attributes)
            
            icon.draw(in:
                CGRect(x: (size.width - iconSize.width)/2.0, y: (size.height - iconSize.height)/2.0, width: iconSize.width, height: iconSize.height),
                      withAttributes: attributes.dictionary)
    

    but dictionary property is internal.

    opened by haashem 5
  • Scope errors with cocoapod

    Scope errors with cocoapod

    I'm using Cocoapods 1.2 and Xcode 8.2.1. I get the following error when trying to use the library: 'font' is inaccessible due to 'internal' protection level I'm getting this error whether I attempt to set the values through in init closure or set them separately on an existing instance.

    Based on my own experience creating libraries with Cocoapods, all properties and methods have to be explicitly marked public even if the parent class/struct is public. Although the NSParagraphStyle related methods in the extension seem to all be marked public, only the init methods in the base class are public.

    opened by bryanbartow 5
  • Could check out Attributed with Carthage

    Could check out Attributed with Carthage

    Attributes.swift:92:47: error: 'NSBaselineOffsetAttributeName' has been renamed to 'NSAttributedStringKey.baselineOffset' return self + Attributes(dictionary: [NSBaselineOffsetAttributeName: offset]) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ NSAttributedStringKey.baselineOffset UIKit.NSBaselineOffsetAttributeName:3:12: note: 'NSBaselineOffsetAttributeName' was obsoleted in Swift 3 public let NSBaselineOffsetAttributeName: NSAttributedStringKey

    opened by dungi 4
  • Attributed 3.0 (And beyond)

    Attributed 3.0 (And beyond)

    Intent of this issue

    Keep track of what should be added and removed from Attributed and which improvements should be made to the existing API.

    TODO

    • [ ] Update the attributes of an existing attributed String
    • [ ] Improve Support for applying attributes to ranges of a string
    • [ ] Improve Documentation in README
    • [ ] Increase code coverage past 70%
    enhancement help wanted 
    opened by Nirma 3
  • Compiling issues

    Compiling issues

    Pods/AttributedLib/Attributed/String+Attributed.swift:33:61: Cannot convert value of type '[NSAttributedStringKey : Any]' (aka 'Dictionary<NSString, Any>') to expected argument type '[String : Any]?'

    Pods/AttributedLib/Attributed/Attributes.swift:48:47: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'kern'

    opened by vstepanyuk 3
  • change an attribute of attributedString

    change an attribute of attributedString

    consider this scenario: I have set an attributedText for UILabel

    then later based on a condition I need only to change the textColor.

    what I'm looking for:

    textLabel.at.change(.foregroundColor).to(.yellow)
    

    whats your opinion?

    what I do right now is:

    var attributes = titleNode.attributedText?.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, titleNode.attributedText!.length))
                
                if isSelected {
                    attributes?[NSAttributedStringKey.foregroundColor] = selectedTitleColor
                } else {
                    attributes?[NSAttributedStringKey.foregroundColor] = normalTitleColor
                }
                
                titleNode.attributedText = NSAttributedString(string: title, attributes: attributes)
    
    opened by haashem 3
  • README: consider removing discussion of stale branches

    README: consider removing discussion of stale branches

    The bottom of the README contains mentions to development, attributed-3.0 branches; but it appears that those are stale and all development is actually happening on master (which is fine). I was confused by this. Please consider removing the stale branches and this information from the README.

    opened by natan 2
  • NSString+Attributed.swift stack oveflow

    NSString+Attributed.swift stack oveflow

    The extension for NSString when used, result on an infinite loop.

    extension Attributed where Base == NSString {
    
        public func attributed(with attributes: Attributes) -> NSAttributedString {
            return attributed(with: attributes)
        }
    
        public func attributed(_ attributeBlock: (Attributes) -> (Attributes)) -> NSAttributedString {
            return attributed(attributeBlock)
        }
    }
    

    I believe the intended behaviour here was to call the String extension, and if so, the could should be:

    extension Attributed where Base == NSString {
    
        public func attributed(with attributes: Attributes) -> NSAttributedString {
            return (self as String).at.attributed(with: attributes)
        }
    
        public func attributed(_ attributeBlock: (Attributes) -> (Attributes)) -> NSAttributedString {
            return (self as String).at.attributed(attributeBlock)
        }
    }
    
    opened by vfn 2
  • Add support for applying attributes within ranges.

    Add support for applying attributes within ranges.

    As the implementation of the library now stands creating an attributed string composed of different attributes can be done with the addition operator like so:

    let baseAttributes = Attributes().font(UIFont(name: "Chalkduster", size: 24.0)!)
    let accentedAttributes = baseAttribues.foreground(color: .red) 
                                          .underlineStyle(.styleSingle)
    
    let partOne = "Hello, ".attributed(with: baseAttributes) 
    let partTwo = "World!".attributed(with: accentedAttributes)
    let attributedString = partOne + partTwo
    

    This works, albeit inconvenient.

    Adding functionality to the library that supports applying attributes to ranges would make this task much easier. Perhaps something like the following:

    let baseAttributes = Attributes().font(UIFont(name: "Chalkduster", size: 24.0)!)
    let accentedAttributes = baseAttribues.foreground(color: .red) 
                                          .underlineStyle(.styleSingle)
    
    "Hello, World!".attributed(with: [(0..5, baseAttributes ), accentedAttributes])
    

    Where the text with different attributes is specified with a range and attributes and then the remainder of the text is attributed with the attributes contained in the tuple with the nil range.

    opened by Nirma 2
  • issue on multitarget podfile

    issue on multitarget podfile

    I encountered this on pod install (pod version 1.9.3)

    [!] Unable to determine Swift version for the following pods:
    
    - `AttributedLib-library` does not specify a Swift version and none of the targets (`PhotographApp`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.
    

    my podfile:

    source 'https://cdn.cocoapods.org/'
    
    platform :ios, '12.0'
    inhibit_all_warnings!
    
    abstract_target 'PhotographApp' do
    
      pod 'AttributedLib', '= 3.0.0'
      pod 'Firebase/Messaging', '= 6.30.0'
      pod 'Intercom', '= 7.1.2'
      pod 'IQKeyboardManagerSwift', '= 6.5.5'
      pod 'JWTDecode', '= 2.4.1'
      pod 'lottie-ios', '= 3.1.5'
      pod 'R.swift', '= 5.2.1'
      pod 'RxSwift', '= 5.1.1'
      pod 'Sentry', '= 5.2.1'
      pod "SkeletonView", '= 1.8.1'
      pod 'SwiftLint', '= 0.38.0'
      pod 'Swinject', '= 2.7.1'
      pod 'TTGSnackbar', '= 1.10.3'
      pod 'Moya', '= 14.0.0'
    
      target 'photograph' do
        use_frameworks!
    
        target 'photographTests' do
          inherit! :search_paths
    
          pod 'Alamofire', '= 5.1.0'
          pod 'Cuckoo', '= 1.3.2'
          pod 'RxTest', '= 5.1.1'
        end
      end
    
      target 'photograph-mock' do
        use_frameworks!
      end
    
    end
    

    I tried the small ruby line script for add swift_version on post_install but its not working. Any hint ?

    opened by SaezChristopher 1
Releases(3.1.0)
  • 3.1.0(Mar 14, 2021)

  • 3.0.0(Apr 8, 2019)

  • 2.2.1(Feb 9, 2019)

  • 2.2.0(Sep 25, 2018)

  • 2.1.1(May 26, 2018)

    This release is a minor release that deals with a crash involving using the underline Attribute. The issue is detailed here: #53

    Great work @SaezChristopher ! Thanks so much!

    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Mar 27, 2018)

    This release makes the dictionary held by the Attributes struct public. The state of this dictionary still can not be edited, but it can now be read.

    Source code(tar.gz)
    Source code(zip)
  • 2.0.2(Oct 30, 2017)

    This release addresses an issue with the the strike-through attribute not properly working, this issue has since been fixed and tested.

    Enjoy!

    Source code(tar.gz)
    Source code(zip)
  • 2.0.1(Sep 23, 2017)

  • 2.0.0(Sep 22, 2017)

  • 1.3.0(Jul 20, 2017)

    Summary

    This release adds functionality for supporting the use of NSBaseLineAttributeName through this library's interface. Just call .baseOffset() with an NSNumber value specifying the offset you wish to use.

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Jun 16, 2017)

    Summary

    This is a purely additive release.

    The new functionality is the addition of the function uniformLineHeight:

    func uniformLineHeight(_ uniformLineHeight: CGFloat) -> Attributes { ... }
    

    This function is to be used when an attributed string has the same value for both minimumLineHeight and maximumLineHeight allowing this to be specified in one line rather than two.

    Side Note

    Up until now I have not been keeping a change log, despite believing in their value when trying to piece together the history of a repository.

    From this release forward there is a proper change log in the root of this repository here.

    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Apr 24, 2017)

    This release includes a bug fix for the attributed extensions to NSAttributedString that was introduced by mistake in the 1.0.0 release.

    Thanks so much @vfn for pointing that out!!

    Also included in this release is a new feature from @hiragram that adds support for using NSLinkAttributeName.

    Thanks @vfn and @hiragram 💯

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Apr 19, 2017)

    This release is mainly just a refactoring of the API but this update is a breaking change, therefore it might be wise to raise the version number to 1.0.0.

    Rather than extending String, NSString and NSAttributedString directly extensions are now made on an Attributed class with the clause where Base == (Type) .

    Basically what was before written as "Hello".attributed(with: attributes) is now written with an .at after the String i.e "Hello".at.attributed(with: attributes)

    Enjoy!

    Source code(tar.gz)
    Source code(zip)
  • 0.2.2(Apr 13, 2017)

  • 0.2.1(Mar 21, 2017)

  • 0.2.0(Feb 24, 2017)

    New Features

    This release provides support for using attributes NSObliquenessAttributeName, NSShadowAttributeName. These attributes can be accessed by using the corresponding shadow(_ shadow: NSShadow) and obliqueness(_ value: CGFloat) methods that were added in this release.

    @EndouMari Thank you for adding this!

    Source code(tar.gz)
    Source code(zip)
  • 0.1.2(Feb 14, 2017)

    This release addresses an error caught by @andreyz in issue #6 and corrected by @QuanSai by pull request #7.

    Thanks so much for the support!!

    Source code(tar.gz)
    Source code(zip)
  • 0.1.1(Feb 8, 2017)

    This release addresses an issue where some of Attributes methods not properly being exposed. Aside from that no other features or tweaks have been introduced.

    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Dec 26, 2016)

Owner
Nicholas Maccharoli
Day job is iOS / Swift dev mostly, Python and Golang enthusiast. Coding with love since 2007.
Nicholas Maccharoli
Texstyle allows you to format iOS attributed strings easily.

Texstyle allows you to format attributed strings easily. Features Applying attributes with strong typing and autocompletion Cache for attributes Subst

Rosberry 79 Sep 9, 2022
An easier way to compose attributed strings

TextAttributes makes it easy to compose attributed strings. let attrs = TextAttributes() .font(name: "HelveticaNeue", size: 16) .foregroundCol

Damien 2.2k Dec 31, 2022
A simple library for building attributed strings, for a more civilized age.

Veneer A simple library for building attributed strings, for a more civilized age. Veneer was created to make creating attributed strings easier to re

Wess Cope 26 Dec 27, 2022
Easiest way to create an attributed UITextView (with support for multiple links and from html)

AttributedTextView Easiest way to create an attributed UITextView (with support for multiple links and html). See the demo app and the playground for

Edwin Vermeer 430 Nov 24, 2022
BonMot is a Swift attributed string library

BonMot (pronounced Bon Mo, French for good word) is a Swift attributed string library. It abstracts away the complexities of the iOS, macOS, tvOS, and

Rightpoint 3.4k Dec 30, 2022
👩‍🎨 Elegant Attributed String composition in Swift sauce

Elegant Attributed String composition in Swift sauce SwiftRichString is a lightweight library which allows to create and manipulate attributed strings

Daniele Margutti 2.9k Jan 5, 2023
More powerful label, attributed string builder and text parser.

DDText More powerful label, attributed string builder and text parser. DDLabel More powerful label than UILabel, using TextKit. It supports features b

Daniel 16 Nov 8, 2022
A quick helper for setting attributed texts to UILabel.

UILabelAttributedTextHelper A quick helper for setting attributed texts to UILabel. Sample usage: label.setAttributedText( leadingText: "H

Glenn Posadas 5 Aug 24, 2022
Easy Attributed String Creator

The main idea of this project is to have an online tool to be able to visually add formatting to a text and get back a swift and/or objective-c code t

Andres Canal 283 Dec 27, 2022
A library for formatting strings on iOS and macOS

Sprinter Introduction What? Why? How? Usage Installation Integration Localization Thread Safety Advanced Usage Introduction What? Sprinter is a librar

Nick Lockwood 168 Feb 6, 2022
🌭 Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it.

Mustard ?? Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it. Quick start using character sets Foundation

Mathew Sanders 695 Nov 11, 2022
Converts Markdown files and strings into NSAttributedStrings with lots of customisation options.

SwiftyMarkdown 1.0 SwiftyMarkdown converts Markdown files and strings into NSAttributedStrings using sensible defaults and a Swift-style syntax. It us

Simon Fairbairn 1.5k Dec 22, 2022
A Swift framework for using custom emoji in strings.

Emojica – a Swift framework for using custom emoji in strings. What does it do? Emojica allows you to replace the standard emoji in your iOS apps with

Dan 101 Nov 7, 2022
Generate SwiftUI Text or AttributedString from markdown strings with custom style names.

iOS 15.0 / macOS 12.0 / tvOS 15.0 / watchOS 8.0 StyledMarkdown is a mini library that lets you define custom styles in code and use them in your local

null 19 Dec 7, 2022
A Swifty API for attributed strings

SwiftyAttributes A Swifty API for attributed strings. With SwiftyAttributes, you can create attributed strings like so: let fancyString = "Hello World

Eddie Kaiger 1.5k Jan 5, 2023
Texstyle allows you to format iOS attributed strings easily.

Texstyle allows you to format attributed strings easily. Features Applying attributes with strong typing and autocompletion Cache for attributes Subst

Rosberry 79 Sep 9, 2022
An easier way to compose attributed strings

TextAttributes makes it easy to compose attributed strings. let attrs = TextAttributes() .font(name: "HelveticaNeue", size: 16) .foregroundCol

Damien 2.2k Dec 31, 2022
A simple library for building attributed strings, for a more civilized age.

Veneer A simple library for building attributed strings, for a more civilized age. Veneer was created to make creating attributed strings easier to re

Wess Cope 26 Dec 27, 2022
JSONHelper - ✌ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

JSONHelper Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to boolea

Baris Sencan 788 Jul 19, 2022
Validate iOS, Android, and Mac localizations. Find errors in .strings, .stringsdict, and strings.xml files.

Locheck An Xcode and Android localization file validator. Make sure your .strings, .stringsdict, and strings.xml files do not have any errors! What do

Asana 73 Dec 13, 2022