【发布时间】:2015-02-19 01:28:55
【问题描述】:
如何在 UITextView 中绘制文本,就像在 UILabel 和 UITextField 中有一个名为“drawTextInRect:”的方法一样。我应该在“drawRect:”还是“drawLayer:inContext:”中实现它。感谢您的关注和耐心!
【问题讨论】:
标签: ios objective-c uiview drawing uitextview
如何在 UITextView 中绘制文本,就像在 UILabel 和 UITextField 中有一个名为“drawTextInRect:”的方法一样。我应该在“drawRect:”还是“drawLayer:inContext:”中实现它。感谢您的关注和耐心!
【问题讨论】:
标签: ios objective-c uiview drawing uitextview
你可以像下面的代码一样使用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];
【讨论】: