作者:荆州房产网 | 来源:互联网 | 2024-10-10 19:52
http:stackoverflow.comquestions19274408drawinrectwithattributes-vs-drawinrectwithfontlineb
http://stackoverflow.com/questions/19274408/drawinrectwithattributes-vs-drawinrectwithfontlinebreakmodealignment
- (void)drawRect:(CGRect)frame
{
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByWordWrapping;
textStyle.alignment = NSTextAlignmentCenter;
UIFont *textFOnt= [UIFont systemFontOfSize:16];
NSString *text = @"Lorem ipsum";
// iOS 7 way
[text drawInRect:frame withAttributes:@{NSFontAttributeName:textFont, NSParagraphStyleAttributeName:textStyle}];
// pre iOS 7 way
CGFloat margin = 16;
CGRect bottomFrame = CGRectMake(0, margin, frame.size.width, frame.size.height - margin);
[text drawInRect:bottomFrame withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}
ios-drawInRect:withAttributes