Trying to make software development less painful and more fun one repository at a time.
If you have any questions or would like to get in contact please feel free to open an issue or find me on twitter.
µframework for Attributed strings.
License: MIT License
Trying to make software development less painful and more fun one repository at a time.
If you have any questions or would like to get in contact please feel free to open an issue or find me on twitter.
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.
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.
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)
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.
Exactly how this would be implemented is still not something that I have worked out.
Still thinking about how the api would work but maybe something like passing an array of (Range, Attribute)
tuples to the attributed(with:)
function.
Any input or help would be awesome.
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)
}
}
This line should say NSBackgroundColorAttributeName
.
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'
Uses of strikthrough attribute like this is no longer working.
attributes.append("Siddarth".at.attributed({
$0.strikeThroughStyle(.styleSingle)
.lineBreakMode(.byWordWrapping)
}))
I think it would be nice to have a simple DSL that would enable users of Attributed to define Attributed strings with even more ease.
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:
So I consider maybe you should move the tag at the correct place.
Keep track of what should be added and removed from Attributed and which improvements
should be made to the existing API.
Pull request #40
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.
Nice helpers, dont let it die and accept 👍
Gj
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
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 ?
I have searched through the documents and files but didn't get any method for NSBaselineOffsetAttributeName. Is it not there?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.