1

I'm using Swift 4, with Xcode 9.2.

I have a UILabel, which in their .attributedText property I have associated an NSMutableAttributedString. In fact, this NSMutableAttributedString is made from the concatenation of two different NSMutableAttributedString, everyone with different attributes.

Since using append() I merge all NSMutableAttributedString in a single mutable string, how can I retrieve the range of the attributes I have previously set before appending everything (probably it can't be done)? Or, there is a way to place more than a NSMutableAttributedString inside the UILabel.attributedText? Or, again, can I get this behaviour without using a UILabel (maybe using a UITextView, for example)?

This is the problem I encountered because I need to add a UISwitch that hides (= restore it to default black) and shows again the foregroundColor of this NSMutableAttributedString on the go, but since only a part of this string has a foregroundColor attribute, I can't simply apply the attribute on the whole string.

This is the code:

func attributedTest (_ testString : String)
{
    // the first NSMutableAttributedString created
    let firstString = NSMutableAttributedString(
        string: testString,
        attributes: [NSAttributedStringKey.font : UIFont(
            name: "Futura",
            size: 20.0)!])

    // the second NSMutableAttributedString created, they're equal
    let secondString = NSMutableAttributedString(
        string: testString,
        attributes: [NSAttributedStringKey.font : UIFont(
            name: "Futura",
            size: 20.0)!])

    // here I'm adding the attributed on test2
    // the NSRange, in this case, is equal to the whole string
    secondString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRangeFromString("0, \(testString.count)"))

    // here I'm appending the second string to the first one
    firstString.append(secondString)

    // here I'm setting the testLabel.attributedText
    testLabel.attributedText = test
}

I thought of a plain copy of this NSMutableAttributedString, with no attributes at all (the UIFont is external to the attribute so I'm not worried), stored somewhere. Can having these two strings and switch them on the go when needed through the UISwitch be a decent solution (not optimal, though)?

Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • U can get range from here.. https://stackoverflow.com/questions/25207373/changing-specific-texts-color-using-nsmutableattributedstring-in-swift – McDonal_11 Feb 21 '18 at 05:02

1 Answers1

0

Try this

     NSMutableAttributedString *mutableAttStr = [[NSMutableAttributedString alloc] init];
     NSString *str = @"your string here";
     NSDictionary *attributes = @{NSForegroundColorAttributeName : [UIColor redColor]};
     NSAttributedString *newAttStr = [[NSAttributedString alloc] initWithString:str attributes:attributes];
    [mutableAttStr appendAttributedString:newAttStr];
    testLabel.attributedText = mutableAttStr;
  • I don't have problems in creating and appending NSMutableAttributedStrings, in fact my code works very well with that. My problem is how to know where attributes are already applied to the string, and delete / restore them on the go – Michele Rega Feb 21 '18 at 10:42
  • You can set range to your string and append final string to mutable object. like " [mutableAttStr addAttribute:str value:[UIColor grayColor] range:NSMakeRange(0, str.length)] " – G. Hazarath Reddy Feb 21 '18 at 12:09