如何在 UITextView 中绘制文本

How to draw text in UITextView

如何在UITextView中绘制文本就像UILabel和UITextField中有一个方法叫做"drawTextInRect:"。我应该在 "drawRect:" 还是 "drawLayer:inContext:" 中实施它?感谢您的专注和耐心!

您可以像下面的代码一样使用drawinrect。

UIFont *font = [UIFont fontWithName:@"Courier" size:kCellFontSize];

/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentRight;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                    NSParagraphStyleAttributeName: paragraphStyle };

[text drawInRect:rect withAttributes:attributes];