gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:29:31 28 4
gpt4 key购买 nike

我尝试在字符串的一部分下划线,例如,'test string' 字符串中的 'string' 部分。我正在使用 NSMutableAttributedString 并且我的解决方案在 iOS7 上运行良好。

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

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

最佳答案

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

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

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 :

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

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

好像是苹果的bug :(

关于ios - 在 iOS 8 中使用 NSMutableAttributedString 的字符串的下划线部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26136157/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com