- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:")
ios 图形与动画学习笔记 绘制文本drawInRect: 相关方法
755发布时间:2015-01-23 15:30 类别:IOS开发
之前使用- (CGSize)drawAtPoint:(CGPoint)point withFont:(UIFont *)font;绘制文本。
使用UIColor设置文本颜色,比如代码:
UIColor *textColor = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
/* Set the color in the graphical context */
[textColor set];
之后设置字体
UIFont *helvetica_Bold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30];
NSString *string = @"我的学习笔记";
之后使用
[string drawInRect:CGRectMake(20, 20, 30, 500) withFont:helvetica_Bold];
具体代码:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//此方法将是我们主要绘图的地方,首先加载地体,在屏幕上绘制一个简单的字符串
/* Load the color */
UIColor *textColor = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
/* Set the color in the graphical context */
[textColor set];
/* Load the font */
/* Our string to be drawn */
UIFont *helvetica_Bold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30];
NSString *string = @"我的学习笔记";
//[string drawAtPoint:CGPointMake(40, 240) withFont:helvetica_Bold];
[string drawInRect:CGRectMake(20, 20, 30, 500) withFont:helvetica_Bold];
}
@interface NSString(UIStringDrawing)
// Single line, no wrapping. Truncation based on the NSLineBreakMode.
- (CGSize)sizeWithFont:(UIFont *)font NS_DEPRECATED_IOS(2_0, 7_0, "Use -sizeWithAttributes:");
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");
// Single line, no wrapping. Truncation based on the NSLineBreakMode.
- (CGSize)drawAtPoint:(CGPoint)point withFont:(UIFont *)font NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawAtPoint:withAttributes:");
- (CGSize)drawAtPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:");
// Wrapping to fit horizontal and vertical size. Text will be wrapped and truncated using the NSLineBreakMode. If the height is less than a line of text, it may return
// a vertical size that is bigger than the one passed in.
// If you size your text using the constrainedToSize: methods below, you should draw the text using the drawInRect: methods using the same line break mode for consistency
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:"); // Uses NSLineBreakModeWordWrap
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:"); // NSTextAlignment is not needed to determine size
// Wrapping to fit horizontal and vertical size.
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:");
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:");
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:");
// These methods will behave identically to the above single line methods if the string will fit in the specified width in the specified font.
// If not, the font size will be reduced until either the string fits or the minimum font size is reached. If the minimum font
// size is reached and the string still won't fit, the string will be truncated and drawn at the minimum font size.
// The first two methods are used together, and the actualFontSize returned in the sizeWithFont method should be passed to the drawAtPoint method.
// The last method will do the sizing calculation and drawing in one operation.
- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat*)actualFontSize forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0);
- (CGSize)drawAtPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font fontSize:(CGFloat)fontSize lineBreakMode:(NSLineBreakMode)lineBreakMode baselineAdjustment:(UIBaselineAdjustment)baselineAdjustment NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:");
- (CGSize)drawAtPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize lineBreakMode:(NSLineBreakMode)lineBreakMode baselineAdjustment:(UIBaselineAdjustment)baselineAdjustment NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:");
NSLineBreakByWordWrapping和NSLineBreakByCharWrapping的异常
在iOS开发过程中,在UILable中lineBreakMode中有以下几个功能
NSLineBreakByWordWrapping = 0 //以空格为边界,保留单词。
NSLineBreakByCharWrapping //保留整个字符
NSLineBreakByClipping //简单剪裁,到边界为止
NSLineBreakByTruncatingHead //前面部分文字以……方式省略,显示尾部文字内容
NSLineBreakByTruncatingTail //结尾部分的内容以……方式省略,显示头的文字内容。
NSLineBreakByTruncatingMiddle //中间的内容以……方式省略,显示头尾的文字内容。
我在使用NSLineBreakByWordWrapping和NSLineBreakByCharWrapping的时候产生了疑问,他们的演示效果完全一样。网上也找不到答案。最后发现原来是sdk版本的问题。我使用的xcode是5.0版本,默认使用的是sdk7.0。使用sdk7.0会导致两者效果完全相同。使用sdk6.1运行的时候可以有效区分开NSLineBreakByWordWrapping和NSLineBreakByCharWrapping。可见苹果正在逐步取消了这一区别,如果你搜索到了这篇文章,那你就使用sdk6.1就可以实现那些功能了。