infinitered / rmq

RMQ - RubyMotionQuery
MIT License
307 stars 52 forks source link

Error when trying to apply attributed text to UILabel #273

Closed andrewhavens closed 9 years ago

andrewhavens commented 9 years ago

I'm using sugarcube to create an attributed string that I am trying to apply to a UILabel using an RMQ stylesheet in a RedPotion project:

class MyStylesheet < ApplicationStylesheet
  def sign_in_text(st)
    # ...
    st.attributed_text = 'Already have an account? ' + 'Sign In'.bold
  end
end

...but I'm getting this error:

`sign_in_text:': can't convert NSConcreteMutableAttributedString into String (TypeError)

Am I doing something wrong? Or is this a bug in RMQ or Sugarcube?

GantMan commented 9 years ago

Hey bud! Sugarcube is friendly but always a bit thick for a single need. Have you considered looking into https://github.com/skellock/motion-mastr ?

ALSO, it looks to me like you're trying to concat a non-attributed string to an attributed string. I don't think you can do that. If you switch it to just this, does it still error?:

class MyStylesheet < ApplicationStylesheet
  def sign_in_text(st)
    # ...
    st.attributed_text = 'Sign In'.bold
  end
end
andrewhavens commented 9 years ago

Ah, you're totally right. I misread the error. The first string is trying to call .to_s in order to concatenate it. I read the docs more closely and it explains that the first string needs to be an attributed string. I ended up with:

st.attributed_text = 'Already have an account? '.attrd + 'Sign In'.bold
GantMan commented 9 years ago

awesome!

andrewhavens commented 9 years ago

@GantMan Thanks again for letting me know about motion-mastr. It wasn't long before I found myself limited by sugarcube's API. I soon switched to motion-mastr and now my code is much more concise and readable:

# sugarcube:
attributed_text = 'Already have an account? '.attrd + 'Sign In'.nsattributedstring(NSFontAttributeName => font.bold_extra_small)
# motion-mastr
attributed_text = mastr('Already have an account? ').add('Sign In', font: font.bold_extra_small).build
skellock commented 9 years ago

\o/

twerth commented 9 years ago

It's a great library @skellock