Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

License

NotificationsYou must be signed in to change notification settings

psharanda/Atributika

Repository files navigation


🚨V5 is now released!🚨

Atributika is a Swift library that provides a simple way to build NSAttributedString from HTML-like text, by identifying and styling tags, links, phone numbers, hashtags etc.

A standaloneAtributikaViews library offers UILabel/UITextView drop-in replacements capable of displaying highlightable and clickable links, with rich customization, and proper accessibility support.

Note

Try my new library for doing Auto Layout, a typesafe reimagination of Visual Format Language:https://github.com/psharanda/FixFlex

Intro

While NSAttributedString is undoubtedly powerful, it's also a low-level API that needs a considerable amount of setup work. If your string is a template and the actual content is only known at runtime, this becomes complicated. When dealing with localizations, constructing NSAttributedString isn't straightforward either.

But wait,Atributika comes to your rescue!

letb=Attrs().font(.boldSystemFont(ofSize:20)).foregroundColor(.red)        label.attributedText="Hello <b>World</b>!!!".style(tags:["b": b]).attributedString

Indeed, that's much simpler.Atributika is easy-to-use, declarative, flexible, and handles the rough edges for you.

Features

Atributika

  • NSAttributedString builder.
  • Detects and styles HTML-liketags using a custom high-speed parser.
  • Detects and styleshashtags andmentions (i.e., #something and @someone).
  • Identifies and styleslinks andphone numbers.
  • Detects and styles regexes and NSDataDetector patterns.
  • Styles the entire string or just specified ranges.
  • Allows all the above to be chained together to parse complex strings!
  • Provides a clean and expressive API to construct styles.
  • Offers a separate set of detection utilities for standalone use.
  • Compatible with iOS, tvOS, watchOS, and macOS.

AtributikaViews

  • Custom views withhighlightable and clickable links.
  • Custom text styles fornormal/highlighted/disabled states.
  • Supports custom highlighting.

V5

V5 is a major rewrite of the project, executed in early 2023. It's not fully compatible with the previous version and requires some manual migration. The introduction of breaking changes was necessary for the project's further evolution.

Here's what's new:

NSAttributedString Building

  • Completely rewritten HTML parser, which fixed a multitude of bugs and improved handling of edge cases.
  • More generic and robust text transforming and attribute fine-tuning APIs.

AttributedLabel / AttributedTextView

  • Moved to a standalone library, independent of Atributika.
  • Offers proper accessibility support.
  • Improved performance and touch handling.
  • AttributedLabel is based on UILabel (lightweight, with vertically-centered text).
  • AttributedTextView is based on UITextView (supports scrolling and text selection, with text aligned to the top of the frame).

New examples have been added to the Demo application, including:

  • Basic web browser powered by AttributedTextView
  • SwiftUI integration
  • Highlightable links for Markdown documents

Examples

Detect and style tags, provide base style for the rest of string, don't forget about special html symbols

letredColor=UIColor(red:(0xD0/255.0), green:(0x02/255.0), blue:(0x1B/255.0), alpha:1.0)leta=Attrs().foregroundColor(redColor)letfont=UIFont(name:"AvenirNext-Regular", size:24)!letgrayColor=UIColor(white:0x66/255.0, alpha:1)letbase=Attrs().font(font).foregroundColor(grayColor)letstr="<a>&lt;a&gt;</a>tributik<a>&lt;/a&gt;</a>".style(tags:["a": a]).styleBase(base).attributedString

Detect and style hashtags and mentions

letstr="#Hello @World!!!".styleHashtags(Attrs().font(.boldSystemFont(ofSize:45))).styleMentions(Attrs().foregroundColor(.red)).attributedString

Detect and style links

letstr="Check this website http://google.com".styleLinks(Attrs().foregroundColor(.blue)).attributedString

Detect and style phone numbers

letstr="Call me (888)555-5512".stylePhoneNumbers(Attrs().foregroundColor(.red)).attributedString

Uber String

letlinks=Attrs().foregroundColor(.blue)letphoneNumbers=Attrs().backgroundColor(.yellow)letmentions=Attrs().font(.italicSystemFont(ofSize:12)).foregroundColor(.black)letb=Attrs().font(.boldSystemFont(ofSize:12))letu=Attrs().underlineStyle(.single)letbase=Attrs().font(.systemFont(ofSize:12)).foregroundColor(.gray)letstr="@all I found <u>really</u> nice framework to manage attributed strings. It is called <b>Atributika</b>. Call me if you want to know more (123)456-7890 #swift #nsattributedstring https://github.com/psharanda/Atributika".style(tags:["u": u,"b": b]).styleMentions(mentions).styleHashtags(links).styleLinks(links).stylePhoneNumbers(phoneNumbers).styleBase(base).attributedString

AttributedLabel

lettweetLabel=AttributedLabel()tweetLabel.numberOfLines=0tweetLabel.highlightedLinkAttributes=Attrs().foregroundColor(.red).attributesletbaseLinkAttrs=Attrs().foregroundColor(.blue)leta=TagTuner{Attrs(baseLinkAttrs).akaLink($0.tag.attributes["href"]??"")}lethashtag=DetectionTuner{Attrs(baseLinkAttrs).akaLink("https://twitter.com/hashtag/\($0.text.replacingOccurrences(of:"#", with:""))")}letmention=DetectionTuner{Attrs(baseLinkAttrs).akaLink("https://twitter.com/\($0.text.replacingOccurrences(of:"@", with:""))")}letlink=DetectionTuner{Attrs(baseLinkAttrs).akaLink($0.text)}lettweet="@all I found <u>really</u> nice framework to manage attributed strings. It is called <b>Atributika</b>. Call me if you want to know more (123)456-7890 #swift #nsattributedstring https://github.com/psharanda/Atributika"tweetLabel.attributedText= tweet.style(tags:["a": a]).styleHashtags(hashtag).styleMentions(mention).styleLinks(link).attributedStringtweetLabel.onLinkTouchUpInside={ _, valiniflet linkStr= valas?String{iflet url=URL(string: linkStr){UIApplication.shared.openURL(url)}}}view.addSubview(tweetLabel)

Requirements

Current version is compatible with:

  • Swift 5.0+
  • iOS 11.0 or later
  • tvOS 11.0 or later
  • watchOS 4.0 or later
  • macOS 10.13 or later

Note:AttributedLabel /AttributedTextView are available only on iOS

Why does Atributika have one 't' in its name?

Because in Belarusian/Russian we have one letter 't' (атрыбутыка/атрибутика). So basically it is transcription, not a real word.

Integration

Add dependency toPackage.swift file.

dependencies:[.package(url:"https://github.com/psharanda/Atributika.git",.upToNextMajor(from:"5.0.0"))]

Carthage

Addgithub "psharanda/Atributika" to yourCartfile

CocoaPods

Atributika is available throughCocoaPods. To installit, simply add the following line to your Podfile:

pod"Atributika"pod"AtributikaViews"

Manual

  1. Add Atributika to you project as a submodule usinggit submodule add https://github.com/psharanda/Atributika.git
  2. Open theAtributika folder & dragAtributika.xcodeproj into your project tree
  3. AddAtributika.framework to your target'sLink Binary with Libraries Build Phase
  4. Import Atributika withimport Atributika and you're ready to go

License

Atributika is available under the MIT license. See the LICENSE file for more info.

About

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp