Underline text in a UITextView

前端 未结 13 1398
情歌与酒
情歌与酒 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:55

    If this is static text, you can underline it in Interface Builder. Make sure to make the text 'Attributed' first by selecting 'Attributed' in the drop down menu:

    enter image description here

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

    If you want to format your text (with underlined words, links, colored words...) I suggest you to use FTCoreText

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

    If you are using iOS 6 then you can use the attributedText attribute of UITextView. Apply underline formatting to the text. You can also set the typingAttributes property to ensure the text that the user types has a specific set of formatting if you wish.

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

    I recommend you to use MFUnderlinedTextView, it will be helpful.

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

    Swift 5. As my UITextView if for inserting text, I created an extension function as bellow.

    extension UITextView {
    
      func underlined() {
        let border = CALayer()
        let width = CGFloat(1.0)
        border.borderColor = UIColor.lightGray.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - 5, width:  self.frame.size.width, height: 1)
        border.borderWidth = width
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 15
        let attributes = [NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : UIColor.darkGray, NSAttributedString.Key.font :  UIFont.systemFont(ofSize: 13)]
        self.attributedText = NSAttributedString(string: self.text, attributes: attributes)
      }
    
    }
    

    The border is drawling the line and the style is adding the spacing between the lines. Usage in your UIView custom layout:

    override func layoutSubviews() {
        super.layoutSubviews()
        self.dateAndTimeInput.underlined()
      }
    

    Image with the result

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

    Try to use NSAttributedString as follows and set in UITextView. This works for iOS6.

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"];
    [attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName 
                       value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] 
                       range:(NSRange){0,[attString length]}];
    

    For more info on NSAttributedString check this How do you use NSAttributedString?

    For eg:-

    textView.attributedText = attString;
    

    From apple documentation on UITextView,

    In iOS 6 and later, this class supports multiple text styles through use of the attributedText property. (Styled text is not supported in earlier versions of iOS.) Setting a value for this property causes the text view to use the style information provided in the attributed string. You can still use the font, textColor, and textAlignment properties to set style attributes, but those properties apply to all of the text in the text view.

    attributedText:

    The styled text displayed by the text view.

    @property(nonatomic,copy) NSAttributedString *attributedText
    

    Discussion: This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and textAlignment properties so that they reflect the style information starting at location 0 in the attributed string.

    0 讨论(0)
提交回复
热议问题