eddiekaiger / swiftyattributes Goto Github PK
View Code? Open in Web Editor NEWA Swifty API for attributed strings
License: MIT License
A Swifty API for attributed strings
License: MIT License
I'm getting a collision with Foundation with the NSAttributedString
method enumerateAttributes
:
Ambiguous reference to member 'enumerateAttribute(_:in:options:using:)'
Hello. 😄 I have a problem in iOS 12, 13.
It is normal for versions iOS 14 and above.
The first time �I open the screen with the NSMutableAttributedString, the withTextColor()
does not apply.
And i also have a problem in withAttributes([.textColor()]
.
When I scroll through the screen with the NSMutableAttributedString and come back, the text color will appear as I wish.
I'd like to know a solution.
Here is a real example of the issue I'm facing:
An attributed text with an icon set in a UIButton
.
let button = UIButton(type: .system)
let attachment = TextAttachment()
attachment.image = MediaActionType.connect.iconImage
let attributedString = "".withAttachment(attachment) + " Connect With \(name)".withTextColor(.brand).withFont(.brandon(style: .medium, size: 16.0))
button.setAttributedTitle(attributedString, for: .normal)
This doesn't show the icon but if works if I use NSMutableAttributedString
directly:
let button = UIButton(type: .system)
let attachment = TextAttachment()
attachment.image = MediaActionType.connect.iconImage
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(NSAttributedString(attachment: attachment))
mutableAttributedString.append(" Connect With \(name)".withTextColor(.brand).withFont(.brandon(style: .medium, size: 16.0)))
button.setAttributedTitle(mutableAttributedString, for: .normal)
I'm using Xcode 13.1, CocoaPods and SwiftyAttributes version is 5.1.1.
Any thoughts?
Thank you.
pod install
: Installing SwiftyAttributes (1.0)
swift3.0 appear Error
We've had this issue in our project preventing Xcode 10 compilation since the release. The error is:
Foundation.NSMutableAttributedString:11:12: note: found this candidate
public init(string str: String, attributes attrs: [NSAttributedStringKey : Any]? = nil)
^
Foundation.NSAttributedString:19:12: note: found this candidate
public init(string str: String, attributes attrs: [NSAttributedStringKey : Any]? = nil)
The usage was pretty basic, something like so:
let string = NSMutableAttributedString(string: "Hello World", attributes: [.strokeColor: UIColor.green])
After digging into it I discovered this doesn't happen unless we're importing SwiftyAttributes. If you duplicate the testInit_withStringAndAttributes
test method, but replace the NSAttributedString
s with NSMutableAttributedString
s you'll get the same error when running the tests.
Attempting to use the convenience initializer on a NSMutableAttributedString
results in the following error:
Cannot convert value of type '[Attribute]' to expected argument type '[NSAttributedStringKey : Any]?'
Taking out the convenience initializer on NSAttributedString
, or giving the attributes parameter a different name resolves the issue.
It's likely that this is a bug with Swift. I'm not sure if you'd rather file a bug with them and see what happens, or resolve the issue within the library now. Changing the attributes parameter would be a breaking change and be cause for a major bump.
Let me know if you're going to take action on this at the library level if ya can, as it is preventing us from building on XC10 :)
Here's the diff w/ the fix and test that I experimented with:
diff --git a/SwiftyAttributes/Sources/common/NSAttributedString+SwiftyAttributes.swift b/SwiftyAttributes/Sources/common/NSAttributedString+SwiftyAttributes.swift
index 76a0b01..6c3a934 100644
--- a/SwiftyAttributes/Sources/common/NSAttributedString+SwiftyAttributes.swift
+++ b/SwiftyAttributes/Sources/common/NSAttributedString+SwiftyAttributes.swift
@@ -30,8 +30,8 @@ extension NSAttributedString {
- parameter str: The string for the new attributed string.
- parameter attributes: The attributes for the new attributed string.
*/
- public convenience init(string str: String, attributes: [Attribute]) {
- self.init(string: str, attributes: dictionary(from: attributes))
+ public convenience init(string str: String, swiftyAttributes attrs: [Attribute]) {
+ self.init(string: str, attributes: dictionary(from: attrs))
}
/**
diff --git a/SwiftyAttributesTests/NSAttributedString_Tests.swift b/SwiftyAttributesTests/NSAttributedString_Tests.swift
index 60947b3..79845e4 100644
--- a/SwiftyAttributesTests/NSAttributedString_Tests.swift
+++ b/SwiftyAttributesTests/NSAttributedString_Tests.swift
@@ -12,7 +12,7 @@ import SwiftyAttributes
class NSAttributedString_Tests: XCTestCase {
func testInit_withStringAndAttributes() {
- let subject = NSAttributedString(string: "Hello World", attributes: [.strokeColor(.green), .strokeWidth(3)])
+ let subject = NSAttributedString(string: "Hello World", swiftyAttributes: [.strokeColor(.green), .strokeWidth(3)])
#if swift(>=4.0)
let expected = NSAttributedString(string: "Hello World", attributes: [.strokeColor: Color.green, .strokeWidth: 3])
#else
diff --git a/SwiftyAttributesTests/NSMutableAttributedString_Tests.swift b/SwiftyAttributesTests/NSMutableAttributedString_Tests.swift
index 5577b2e..f95aba3 100644
--- a/SwiftyAttributesTests/NSMutableAttributedString_Tests.swift
+++ b/SwiftyAttributesTests/NSMutableAttributedString_Tests.swift
@@ -10,6 +10,16 @@ import XCTest
import SwiftyAttributes
class NSMutableAttributedString_Tests: XCTestCase {
+
+ func testInitMutable_withStringAndAttributes() {
+ let subject = NSMutableAttributedString(string: "Hello World", swiftyAttributes: [.strokeColor(.green), .strokeWidth(3)])
+ #if swift(>=4.0)
+ let expected = NSMutableAttributedString(string: "Hello World", attributes: [.strokeColor: Color.green, .strokeWidth: 3])
+ #else
+ let expected = NSMutableAttributedString(string: "Hello World", attributes: [NSStrokeColorAttributeName: Color.green, NSStrokeWidthAttributeName: 3])
+ #endif
+ XCTAssertEqual(subject, expected)
+ }
func testAddAttributes_usingSwiftRange() {
let subject = "Hello".withTextColor(.orange)
Since Xcode 11 includes Swift Package natively, it would be nice to be able to include SwiftyAttributes using SPM !
Hi Eddie,
I got this error when i wanna compile the app.
Pods/SwiftyAttributes/SwiftyAttributes/Sources/common/NSAttributedString+SwiftyAttributes.swift:95:29: Generic parameter 'Element' could not be inferred
Please convert to swift 4, i tried but facing some problems
Build warning is always there:
/Users/jeffreyblayney/Library/Developer/Xcode/DerivedData/PracticePlayer-bcsqogsyamkqalgkuggzxalocanz/SourcePackages/checkouts/SwiftyAttributes/Package.swift The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.5.99.
Should update deployment target to 9.0 so this goes away.
Hi there,
The following line, copy pasted, doesn't seem to work. Swift 4.
let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18))
Error is:
Binary operator '+' cannot be applied to two 'NSMutableAttributedString' operands
Would you be willing to add support for swift package manager?
Hi, when i use your example "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18)) i get only text with font size 12. It happens in uitableviewcell with autoheight
Hello.
I found out that currently in SwiftyAttributes there are different prefixes for the Swift Standard Library properties and functions wrappers. For example, UINavigationBar.swiftyTitleTextAttributes
starts with swifty
and NSString.swifty_size(withSwiftyAttributes:)
starts with swifty_
. Would you like to unify these prefixes? For example, I can rename NSString.swifty_size(withSwiftyAttributes:)
to NSString.swiftySize(withSwiftyAttributes:)
.
When working with NSAttributedStrings
that use custom NSAttributedStringKey
values, SwiftyAttributes crashes whenever attributes are accessed.
Here's one way to reproduce:
let attributedString = NSMutableAttributedString(string: "hello world")
attributedString.setAttributes([NSAttributedStringKey("crash"): "bang"], range: NSRange(..<attributedString.length))
attributedString.enumerateSwiftyAttributes(in: 0..<attributedString.length) { attributes, _, _ in
debugPrint(attributes)
}
I realize that type-safety for a finite number of Foundation
values is part of SwiftyAttributes's appeal, but I also think this crash should be handled by the library due to the existence of NSAttributedStringKey(string:)
.
I haven't given too much thought into how I think this could be handled, but perhaps something like:
.custom([String: Any])
is an appropriate solution for any NSAttributedStringKey
values that can't be constructed via NSAttributedStringKey(rawValue:)
.
FWIW, this came up due to some work I'm doing with markdown rendering, where we're storing the URL for links in a custom attribute as part of the attributed string.
Will this repository be maintained?
Hey @eddiekaiger, first off thanks for the really useful library -- It's already been a huge timesaver for me.
Maybe I'm overlooking it, but is there a convenient way to go from [Attribute]
to [String: Any]
?
I keep running into situations like this:
let attributes: [Attribute] = [.font(UIFont.systemFont(ofSize: 15.0)), .textColor(.red)]
UINavigationBar.appearance().titleTextAttributes = ??
An extension of [Attribute]
would be a pretty awesome way to handle this (if it's even possible). Thoughts?
Hello
I'm using this library on macOS with Swift for a project. So, basically, I want to get the color of the underlined text in my NSTextStorage of my NSTextView. This library is very good to create attributes, modify and even delete, but I wonder how to access the color.
Thanks any advance for your ideas. 😄
See this test
let r1 = "Hi ".withTextColor(.black).withFont(UIFont(name: "Futura-Medium", size: 10.0)!)
let r2 = "Google".withTextColor(.black).withUnderlineStyle(.styleSingle).withFont(UIFont(name: "Futura-Medium", size: 10.0)!).withLink(URL(string: "https://www.google.com")!)
let r3 = " and ".withTextColor(.black).withFont(UIFont(name: "Futura-Medium", size: 10.0)!)
let r4 = "Yahoo".withTextColor(.black).withUnderlineStyle(.styleSingle).withFont(UIFont(name: "Futura-Medium", size: 10.0)!).withLink(URL(string: "https://www.yahoo.com")!)
blaBlaBla.attributedText = r1 + r2 + r3 + r4
blaBlaBla.isUserInteractionEnabled = true
Links are not clickable.
Xcode can't autocomplete when i'm typing something like this
static let boldWithRedColor : [SwiftyAttributes.Attribute]! = [
.font(UIFont.boldSystemFont(ofSize: 14))
.textColor(UIColor.red)
]
Anyone has a fix for it ?
Hi,
In some cases, I'd like to execute a method on click of label. Currently I am using the following method.
@objc func tapTermsLabel(gesture: UITapGestureRecognizer) {
let text = (self.termsLabel.text)!
var termsRange = (text as NSString).range(of: "Terms of Service")
var privacyRange = (text as NSString).range(of: "Privacy Policy")
if gesture.didTapAttributedTextInLabel(label: self.termsLabel, inRange: termsRange) {
let viewController = WebViewViewController(url: APIEndPoint.get(type: "terms-of-service-\(getUserLocale())"))
present(viewController, animated: true, completion: nil)
} else if gesture.didTapAttributedTextInLabel(label: self.termsLabel, inRange: privacyRange) {
let viewController = WebViewViewController(url: APIEndPoint.get(type: "privacy-policy-\(getUserLocale())"))
present(viewController, animated: true, completion: nil)
}
}
Although this works. It would be nice to have something similar integrated with the library for example
"Terms of Service".onTap(#selector(methodName))
Can this feature request be considered?
Thanks.
Hi, thanks for great library, but i have a problem i can't change ink color it is always default blue.
even if i make like with
attributedString += text.withLink(.init(string: destination)!)
.withFont(.boldSystemFont(ofSize: 15))
.withTextColor(.green)
it has no effect
I added SwiftyAttributes to my project with cocoapods but + cant work
let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + "World!".withFont(.systemFont(ofSize: 18))
Ambiguous use of operator '+'
Would be pretty sweet to have methods that set attributes on the whole range. Override existing methods without a range param will set attributes for the whole range.
func addAttributes(_ attributes: [Attribute])
func addAttributes(_ attributes: [Attribute])
func setAttributes(_ attributes: [Attribute])
func setAttributes(_ attributes: [Attribute])
Hi,
I'm wondering if it is possible to change the case of floating placeholder text.
Thanks,
Hassan
While concatenating two attributed strings, just like in the readme :
let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18))
The compiler produces an error stating that "Binary operator '+' cannot be applied to two 'NSMutableAttributedString' operands", and fancyString
shows as an <<< Error type >>>
.
Swift 3, Xcode 8.2, SwiftyAttributes 3.1.0 with CocoaPods.
Hey there!
If I have time I might bust this out myself, but if you're in and working on something, it would be really cool to have a simple var extension of NSAttributedString:
var mutable: NSMutableAttributedString
within NSAttributedString
Which would basically return a mutable equivalent of the attributed string.
Best!
Installed via cocoa pods, got the error above.
Disappeared after adding
public typealias SpellingState = NSAttributedString.SpellingState
in Attribute.swift:L15.
Is possible entry an HTML string and get an attributed?
CoreText note: Client requested name ".SFUI-Regular", it will get TimesNewRomanPSMT rather than the intended font. All system UI font access should be through proper APIs such as CTFontCreateUIFontForLanguage() or +[UIFont systemFontOfSize:].
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.