Kli*_*akM 59 iphone objective-c ios nsmutableattributedstring ios8
我尝试为字符串的一部分加下划线,例如,' test string '字符串中的' string '部分.我正在使用,我的解决方案运作良好.NSMutableAttributedString
iOS7
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString:@"test string"];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(5, 6)];
myLabel.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
问题是我的解决方案iOS8
不再适用了.在花了一个小时测试多个变体之后NSMutableAttributedString
,我发现这个解决方案只在范围从0开始时才有效(长度可以不同).这是什么原因?我该如何解决这个问题?
kel*_*lin 131
更新: 通过调查此问题:在iOS 8上显示NSMutableAttributedString我终于找到了解决方案!
您应该在字符串的开头添加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]))
Run Code Online (Sandbox Code Playgroud)
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"]];
Run Code Online (Sandbox Code Playgroud)
这种方法的另一个好处是没有任何范围.非常适合本地化的字符串.
好像是苹果虫:(
小智 17
我发现如果你将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
}
Run Code Online (Sandbox Code Playgroud)
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];
Run Code Online (Sandbox Code Playgroud)
它对我有用
现在是 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)
}
}
Run Code Online (Sandbox Code Playgroud)
用法: myAttributedString.underline(term: "some term")
归档时间: |
|
查看次数: |
45035 次 |
最近记录: |