如何创建一个下划线文字的按钮?

时间:2014-04-14 21:14:39

标签: ios uitextview

我创建了一个需要为selectedElement中的文本加下划线的按​​钮。

到目前为止,我有这个代码,但只有在突出显示文本然后按下划线时它才有效。我需要它来强调下划线而不必突出显示文本。如果你再次按下划线按钮,任何有关如何撤消下划线的帮助也很受欢迎。非常感谢您的参与!我是新手):

-(void)underlineTextButton{
    UITextView selectedText = (UITextView ) selectedElement;
    NSRange range = selectedText.selectedRange;
    NSTextStorage *textStorage = selectedText.textStorage;
    [textStorage addAttribute: NSUnderlineStyleAttributeName
    value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
    range:range];
}

1 个答案:

答案 0 :(得分:0)

你可以使用:

-(void) viewDidLoad
{
   //If it gonna be the static text
   _textView.attributedText = [[NSAttributedText alloc] initWithString:@"Your <u>underlined</u> text goes here.."];

  // If need to by text using regex
  NSRange r;
  NSString *s = _textView.text;
  while ((r = [s rangeOfString:@"patternText" options:NSRegularExpressionSearch]).location != NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:[NSString stringWithFormat:@"<u>%@</u>", patternText]];
  _textView.attributtedText = [[NSAttributedText alloc] initWithString:s];

}
相关问题