66

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

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

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

4

8 回答 8

150

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

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

斯威夫特 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]))

目标-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"]];

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

好像是苹果的错误:(

于 2014-10-01T08:04:12.447 回答
22

我发现如果将 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
}
于 2015-03-15T11:25:52.127 回答
6
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];

它对我有用

于 2015-01-29T08:42:31.990 回答
5

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

这是一个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)
    }
}

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

于 2018-09-26T10:51:56.693 回答
2

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

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
    }   
}

用法:

 let myUnderLinedText = "Hello World"
 let underLinedMutableString =  NSMutableAttributedString(string: myUnderLinedText, attributes: titleAttributes).underline(term: myUnderLinedText)
于 2019-08-02T08:05:51.180 回答
1
let values = NSMutableAttributedString(string: "**YourString**")
let range = NSRange(location: 0, length: values.length)
values.addAttribute(.underlineStyle, value: 1, range: range)

这将使字符串具有下划线样式,您也可以替换 .underlinestyle 并使用 .link 将其显示为蓝色的超链接

于 2021-12-07T17:52:53.503 回答
0

我在操场/模拟器中使用了以下扩展(使用 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"])
于 2016-05-29T01:46:38.140 回答
-2

下划线属性加颜色:​​​​

[attributedString addAttribute:NSUnderlineColorAttributeName
                     value:[UIColor redColor]
                     range:NSMakeRange(5, 6)];
于 2014-10-01T07:32:47.090 回答