Underline text in a UITextView

前端 未结 13 1397
情歌与酒
情歌与酒 2020-12-14 23:37

How can I underline text in a UITextView. I understand that I would need to create a subclass of UITextView, but what would go under drawRect

相关标签:
13条回答
  • 2020-12-14 23:40

    I recommend you to use CoreText. A Basic tutorial is here on raywenderlich.

    0 讨论(0)
  • 2020-12-14 23:41

    If you want to avoid having to include CoreText, you can utilize an attributed string with this attribute:

    @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}
    
    0 讨论(0)
  • 2020-12-14 23:45
    textViewMessage.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]};
    
    0 讨论(0)
  • 2020-12-14 23:50
    -(IBAction)underline:(id)sender
    {
        NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
        texts.attributedText = [[NSAttributedString alloc] initWithString:texts.text
                                                               attributes:underlineAttribute];
    }
    
    0 讨论(0)
  • 2020-12-14 23:51

    To underline a text, you have to go where you can select copy, cut , delete options there are now more options like B/I/U( bold, italic, underline). Choose this option and this is it. And to unable it, choose underline option again.

    0 讨论(0)
  • 2020-12-14 23:53

    You can't use "kCTUnderlineStyleAttributeName" or "kCTUnderlineStyleSingle" Now you must do it like this:

        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Text"];
    [attString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){0,[attString length]}];
    
    0 讨论(0)
提交回复
热议问题