如何使用NSAttributedString objective-c为字符串的下划线加下划线

use*_*829 4 objective-c nsattributedstring ios

仅在范围从零开始时,仅对字符串进行下划线.如果我从1开始它然后它的工作原理.绿色无论如何都有效.

+ (NSAttributedString*)returnNSAttributedString:(NSString*)string range:(NSRange)range WithColour:(UIColor*)colour WithUnderLine:(BOOL)underline {
    NSMutableAttributedString *attributedString =
    [[NSMutableAttributedString alloc] initWithString:string];
    if (underline) {
        [attributedString addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:range];
    }
    [attributedString addAttribute:NSForegroundColorAttributeName value:colour range:range];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSRangeFromString(string)];
    return attributedString;
}
Run Code Online (Sandbox Code Playgroud)

它适用于iOS 7而不是iOS 8.

Shu*_*ndu 10

您可以使用NSUnderlineStyleAttributeName和NSUnderlineColorAttributeName属性.你可以像这样使用它:

NSRange foundRange = [wordString rangeOfString:@"Base Mix"];
if (foundRange.location != NSNotFound)
{
    [wordString beginEditing];
    [wordString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:foundRange];
    [wordString addAttribute:NSUnderlineColorAttributeName value:[NSColor redColor] range:foundRange];
    [wordString endEditing];
}
Run Code Online (Sandbox Code Playgroud)