Search code examples
iosobjective-cwatchkitapple-watchxcode-6.2

Cannot use NSAttributedString on WKInterfaceLabel


I want to display certain characters in a WKInterfaceLabel as underlined.

When the range location is 0 (= the beginning of the string), it works correctly, regardless of the length - the characters in the range are underlined.

But setting the location to any other value than 0 does not work - nothing is underlined.

NSMutableAttributedString *attrString=[[NSMutableAttributedString alloc] initWithString:@"123.45"];
NSInteger attrStringLength =[attrString length];

NSRange range = NSMakeRange(0, 1);                // works
NSRange range = NSMakeRange(0, 2);                // works
NSRange range = NSMakeRange(0, attrStringLength); // works
NSRange range = NSMakeRange(1, 1); // does not work
NSRange range = NSMakeRange(2, 2); // does not work

[attrString addAttributes:@{
                            NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) // style
                            }
                    range:range];

[self.valueLabel setAttributedText:attrString];

Does anybody else have this problem? I'm on Xcode 6.2 iOS 8.2 and testing in the simulator.


Solution

  • Its working perfectly with same configuration which you have provided.

    May be there is issue of location and length in case dynamic string assign, you may not getting enough length of text which you are trying to make underlined.