在 iOS 8 中使用 NSMutableAttributedString 的字符串的下划线部分不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26136157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:56:51  来源:igfitidea点击:

Underline part of a string using NSMutableAttributedString in iOS 8 is not working

iosobjective-ciphoneios8nsmutableattributedstring

提问by KlimczakM

I try to underline part of a string, for example, a 'string' part in 'test string' string. I'm using NSMutableAttributedStringand my solution was working well on iOS7.

我尝试在字符串的一部分下划线,例如,“测试字符串”字符串中的“字符串”部分。我正在使用并且我的解决方案在.NSMutableAttributedStringiOS7

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
        initWithString:@"test string"];
[attributedString addAttribute:NSUnderlineStyleAttributeName
                         value:@(NSUnderlineStyleSingle)
                         range:NSMakeRange(5, 6)];
myLabel.attributedText = attributedString;

The problem is that my solution is not working in iOS8anymore. After spending an hour on testing multiple variants of NSMutableAttributedString, I found out that this solution works only when range starts with 0 (length can differ). What is the reason for that? How can I workaround this?

问题是我的解决方案iOS8不再适用。花了一个小时测试了 的多个变体后NSMutableAttributedString,我发现这个解决方案只有在范围从 0 开始时才有效(长度可以不同)。这是什么原因?我该如何解决这个问题?

回答by kelin

Update:By investigating this question: Displaying NSMutableAttributedString on iOS 8I finally found the solution!

更新:通过调查这个问题:Displaying NSMutableAttributedString on iOS 8我终于找到了解决方案!

You should add NSUnderlineStyleNone at the beginning of the string.

您应该在字符串的开头添加 NSUnderlineStyleNone。

Swift 4.2 (nonewas removed):

Swift 4.2(none已删除):

let attributedString = NSMutableAttributedString()
attributedString.append(NSAttributedString(string: "test ",
                                           attributes: [.underlineStyle: 0]))
attributedString.append(NSAttributedString(string: "s",
                                           attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue]))
attributedString.append(NSAttributedString(string: "tring",
                                           attributes: [.underlineStyle: 0]))

Objective-C:

目标-C:

 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
 [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"test "
                                                                          attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}]];
 [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"s"
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
                                                                                      NSBackgroundColorAttributeName: [UIColor clearColor]}]];
 [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"tring"]];

Another bonus of such approach is absence of any ranges. Very nice for localized strings.

这种方法的另一个好处是没有任何范围。非常适合本地化的字符串。

Seems like it is Apple bug :(

好像是苹果的bug :(

回答by exidy

I found that if you apply UnderlineStyleNone to the whole string you can then selectively apply underline to a part that starts in the middle:

我发现如果将 UnderlineStyleNone 应用于整个字符串,则可以有选择地将下划线应用于从中间开始的部分:

func underlinedString(string: NSString, term: NSString) -> NSAttributedString {
    let output = NSMutableAttributedString(string: string)
    let underlineRange = string.rangeOfString(term)
    output.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleNone.rawValue, range: NSMakeRange(0, string.length))
    output.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: underlineRange)

    return output
}

回答by Tanuj Jagoori

NSMutableAttributedString *signUpString = [[NSMutableAttributedString alloc] initWithString:@"Not a member yet?Sign Up now"];

[signUpString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}]];

[signUpString addAttributes: @{NSForegroundColorAttributeName:UIColorFromRGB(0x43484B),NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(17,11)];

signUpLbl.attributedText = [signUpString copy];

It worked for me

它对我有用

回答by Joss

It's September 2018, so this answer is not about iOS8 but it still relates to underlining part of a string.

现在是 2018 年 9 月,所以这个答案与 iOS8 无关,但它仍然与字符串的下划线部分有关。

Here is a Swift 4extension that underlines a given term within an already composed myAttributedString

这是一个Swift 4扩展,它强调了一个已经组成的术语中的给定术语myAttributedString

extension NSMutableAttributedString {

    func underline(term: String) {

        guard let underlineRange = string.range(of: term) else {

            return
        }

        let startPosition = string.distance(from: term.startIndex, to: underlineRange.lowerBound)
        let nsrange = NSRange(location: startPosition, length: term.count)

        addAttribute(
            .underlineStyle,
            value: NSUnderlineStyle.styleSingle.rawValue,
            range: nsrange)
    }
}

Usage: myAttributedString.underline(term: "some term")

用法: myAttributedString.underline(term: "some term")

回答by Mussa Charles

Swift 5 versionof @Joss's answer with few modifications, by adding a returned NSMutableAttributedString, because I couldn't use the original solution without it.

通过添加返回的 NSMutableAttributedString,@Joss 答案的Swift 5 版本几乎没有修改,因为没有它我无法使用原始解决方案。

extension NSMutableAttributedString {

func underline(term: String) -> NSMutableAttributedString {
    guard let underlineRange = string.range(of: term) else {
        return NSMutableAttributedString()
    }
    let startPosition = string.distance(from: term.startIndex, to: underlineRange.lowerBound)
    let nsrange = NSRange(location: startPosition, length: term.count)
    addAttribute(
        .underlineStyle,
        value: NSUnderlineStyle.single.rawValue,
        range: nsrange)
    return self
    }   
}

Usage:

用法:

 let myUnderLinedText = "Hello World"
 let underLinedMutableString =  NSMutableAttributedString(string: myUnderLinedText, attributes: titleAttributes).underline(term: myUnderLinedText)

回答by Atka

I used the following extension (using exidy's function) in playground/simulator and it worked fine , you may change/add attributes depending on your needs

我在操场/模拟器中使用了以下扩展(使用 exidy 的功能)并且它运行良好,您可以根据需要更改/添加属性

 extension NSMutableAttributedString
{


    func changeWordsColour(terms:[NSString])
{
    let string = self.string as NSString
    self.addAttribute(NSForegroundColorAttributeName, value: UIColor.brownColor(), range: NSMakeRange(0, self.length))
    for term in terms
    {
        let underlineRange = string.rangeOfString(term as String)
        self.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: underlineRange)

    }
}
}

 let myStr = NSMutableAttributedString(string: "Change Words Colour")
 myStr.changeWordsColour(["change","Colour"])

回答by darkdongdong

Add color for underline attribute:​​​

为下划线属性添加颜色:

[attributedString addAttribute:NSUnderlineColorAttributeName
                     value:[UIColor redColor]
                     range:NSMakeRange(5, 6)];