Code Monkey home page Code Monkey logo

swiftyattributes's People

Contributors

awilliams88 avatar basthomas avatar dependabot[bot] avatar eddiekaiger avatar emiliopelaez avatar joeferrucci avatar ketzusaka avatar latyntsev avatar romanpodymov 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftyattributes's Issues

In iOS12, 13, the withTextColor does not display text immediately

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.

Assigning a TextAttachment does not apply the image I assigned in the attachment

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.

Instantiating an NSMutableAttributedString while importing SwiftyAttributes causes build error on Xcode 10

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 NSAttributedStrings with NSMutableAttributedStrings 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)

Swift Package Manager support

Since Xcode 11 includes Swift Package natively, it would be nice to be able to include SwiftyAttributes using SPM !

Update Target Version to 9.0

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.

Readme code not working

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

Unified prefix for all wrappers

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:).

Crashes with custom NSAttributedStringKey

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.

Convert an array of attributes to a dictionary

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?

Get color of underlined text

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. 😄

withLink Fails

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.

AutoComplete Doesn't work

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 ?

Invoke a method on tap

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.

Change link color?

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

Ambiguous use of operator '+'

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 '+'

Feature Request: setAttribute and addAttribute overrides w/o range

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])

Binary operator '+' cannot be applied to two 'NSMutableAttributedString' operands

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.

Feature Request: Add mutable extension to NSAttributedString

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!

Support HTML

Is possible entry an HTML string and get an attributed?

iOS 13设备出现的问题

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:].

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.