【问题标题】:Set Text to be Underline without Selection将文本设置为没有选择的下划线
【发布时间】:2012-10-01 18:39:30
【问题描述】:

我试图让用户能够将他们将键入的文本设置为下划线,而无需当前选择文本。这适用于 iOS 6 应用程序,在 UITextView 中输入文本。它将被保存为 NSAttributedString。粗体和斜体工作正常。下划线的某些东西使它无法正常工作。

UITextView *textView = [self noteTextView];
NSMutableDictionary *typingAttributes = [[textView typingAttributes] mutableCopy];
[typingAttributes setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
NSLog(@"attributes after: %@", typingAttributes);
[textView setTypingAttributes:typingAttributes];
NSLog(@"text view attributes after: %@", [textView typingAttributes]);

我的初始日志语句表明它设置为下划线:

attributes after: {
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICFFont: 0xa9c5e30> font-family: \"Verdana\"; font-weight: normal; font-style: normal; font-size: 17px";
    NSKern = 0;
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
    NSUnderline = 1;
}

但是紧随其后的日志语句没有显示 nsunderline 属性。删除 textView setTypingAttributes 行没有任何影响。

text view attributes after: {
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICFFont: 0xa9c5e30> font-family: \"Verdana\"; font-weight: normal; font-style: normal; font-size: 17px";
    NSKern = 0;
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}

我很困惑为什么我让它为粗体和斜体工作,而不是下划线。还有为什么它似乎最初获得属性,然后忘记它。请分享您可能有的任何见解。谢谢。

【问题讨论】:

  • 尝试使用系统字体。不确定是下划线还是字体中的字形。
  • 有完全相同的问题,它保留了我尝试过的所有属性,除了下划线(我尝试过字体、背景色、前景色、文本对齐等,它们都可以正常工作)。您找到解决方法了吗?

标签: ios ios6 nsattributedstring underline


【解决方案1】:

我认为您发现了一个错误,或者至少是一些未记录的行为。如果我将打字属性设置为红色,我可以做到:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
        replacementString:(NSString *)string {
    NSDictionary* d = textField.typingAttributes;
    NSLog(@"%@", d);
    NSMutableDictionary* md = [NSMutableDictionary dictionaryWithDictionary:d];
    // md[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleSingle);
    md[NSForegroundColorAttributeName] = [UIColor redColor];
    textField.typingAttributes = md;
    return YES;
}

使用该代码,用户的所有新输入都会显示为红色。但是,如果我取消注释注释行,尝试在键入属性中添加下划线,它会破坏整个事情 - 我没有下划线,我也没有得到红色!

不过, 记录了您问题另一部分的答案。您必须按照我的方式进行操作,在用户输入时重新声明输入属性,因为正如文档明确指出的那样,“当文本字段的选择发生更改时,字典的内容会自动清除”(这就是你问的“忘记”)。

【讨论】:

  • 感谢马特的回复。我以为只有我!看了你的帖子,做了很多测试。正如您所提到的,下划线不能用作使用 typingAttributes 的预格式化选项。删除线也没有。我尝试过的所有其他方法(前景色和背景色)似乎都有效。
  • 谢谢,DenVog - 我为此提交了一个错误,我建议你也这样做。
  • 我也提交了一个错误。尽管从 6 月份开始我还有一些营业,但我不会屏息以待很快得到修复。
  • 我的许多错误已经开放多年。重要的是报告的数量。苹果明确表示,他们会更加关注被报告次数较多的错误。
  • 我遇到了同样的问题,尝试报告错误,但错误报告页面中有错误并给我这个错误“发生错误”。我如何报告与他们的 bug-reporter 相关的这个 bug? :)
【解决方案2】:

从 iOS 6 开始,UITextView 现在声明了属性属性文本,它允许您通过创建 NSAttributedString 来为文本添加下划线。这是修改后的代码:

UITextView *textView = [self noteTextView];
NSMutableDictionary *typingAttributes = [[textView typingAttributes] mutableCopy];
[typingAttributes setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
NSLog(@"attributes after: %@", typingAttributes);
textView.attributedText = [[NSAttributedString alloc] initWithString:[textView text] attributes:typingAttributes];
NSLog(@"text view attributes after: %@", [textView typingAttributes]);

通过使用此代码,键入的任何其他文本也将符合 NSAttributedString 中设置的格式,例如下划线

希望这会有所帮助!

【讨论】:

  • 你是正确的关于使用 typingAttributes 选择长度为零。但我发现这不适用于下划线或删除线。
猜你喜欢
  • 2015-09-21
  • 2014-11-24
  • 2020-07-26
  • 2021-09-24
  • 2018-04-29
  • 1970-01-01
  • 2011-08-04
  • 2023-01-22
  • 2012-10-10
相关资源
最近更新 更多