Extract string from NSAttributedString

Discussion in 'iOS Programming' started by killerham, Jun 19, 2011.

  1. killerham macrumors newbie

    Joined:
    Sep 12, 2009
    #1
    I'm trying to extract the string from NSAttributedString as an NSString like this:
    Code:
    // Extract the attributed label's(label) text 
    NSString *text = [[NSString alloc] initWithString: [label string]];
    
    But this doesn't work. There are no errors its just as if [label string] returns nothing. Any help on extracting the string from the attributed string will be appreciated.

    Thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    A NSAttributedString is not a NSString or a subclass of NSString. So it is clear that what you are trying to do will never work. If you open the NSAttributedString documentation there is an obvious, documented, method that does what you want/
     
  3. killerham thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #3
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    So what is label (what class is that object an instance of)?
     
  5. killerham thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #5
    Its an instance of NSAttributedString which was taken from the OHAttributedLabel.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    It should not matter where it was taken from. If it's a NSAttributedString (or subclass of the even) all the methods should work. What makes you believe that you are not getting the correct value when you use the label method? Have you displayed or printed the string? Are you sure label is not null? If you NSLog the length of label what is it?
     
  7. killerham thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #7
    The length of the returned string is 0. I know that there is a string from the OHAttributedLabel I'm getting it from but it isn't getting it properly.

    *edit* Here is the block of code im using to get the NSAtrributedString from OHAttributedLabel
    Code:
    NSAttributedString* labelCopy = label.attributedText;
    NSString *text = [[NSString alloc] initWithString: [labelCopy string]];
    NSLog(@"%u",text.length);
    
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    When you say the returned string you mean:
    Code:
    NSLog("%d",[[label string] length])
    
    or

    Code:
    NSLog("%d",[label length])
    
    Also is label nil or not? As sending pretty much any message to a nil object will return 0
     
  9. killerham thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #9
    Sorry I'm an idiot. I was resetting the text before and it was obviously making the text nil. Thanks so much for your help!
     
  10. Gutwrench macrumors 65816

    Gutwrench

    Joined:
    Jan 2, 2011
    #10
    Don't feel bad it happens to all of us. It brings to mind an old Chinese adage that translates to the effective, 'Sometimes even the monkey falls out of the tree.'
     

Share This Page