简体   繁体   English

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

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

I try to underline part of a string, for example, a ' string ' part in ' test string ' string.我尝试在字符串的一部分下划线,例如,“测试字符串”字符串中的“字符串”部分。 I'm using NSMutableAttributedString and my solution was working well on iOS7 .我正在使用NSMutableAttributedString并且我的解决方案在iOS7上运行良好。

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 iOS8 anymore.问题是我的解决方案不再适用于iOS8 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).在花了一个小时测试NSMutableAttributedString多个变体后,我发现这个解决方案只有在范围从 0 开始时才有效(长度可以不同)。 What is the reason for that?这是什么原因? How can I workaround this?我该如何解决这个问题?

Update: By investigating this question: Displaying NSMutableAttributedString on iOS 8 I finally found the solution!更新:通过调查这个问题: Displaying NSMutableAttributedString on iOS 8我终于找到了解决方案!

You should add NSUnderlineStyleNone at the beginning of the string.您应该在字符串的开头添加 NSUnderlineStyleNone。

Swift 4.2 ( none was 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 :(

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
}
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它对我有用

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 4 extension 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")

Swift 5 version of @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)

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"])
let values = NSMutableAttributedString(string: "**YourString**")
let range = NSRange(location: 0, length: values.length)
values.addAttribute(.underlineStyle, value: 1, range: range)

This will make the string with underlined style, also you can replace the .underlinestyle and use .link to show it as a hyperlink in blue color这将使字符串带有下划线样式,您也可以替换 .underlinestyle 并使用 .link 将其显示为蓝色超链接

Add color for underline attribute:​​​为下划线属性添加颜色:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM