如何在UITextView中绘制文本,就像在UILabel和UITextField中有一个名为“drawTextInRect:”的方法一样。我应该在“drawRect:”或“drawLayer:inContext:”中实现它。感谢您的关注和耐心!
答案 0 :(得分:2)
你可以像下面的代码一样使用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];