如何使用NSString drawInRect来居中文本?
我怎样才能绘制一个NSString
居中在一个NSRect
?
我已经开始了:(从我的自定义视图的drawRect方法摘录)
NSString* theString = ... [theString drawInRect:theRect withAttributes:0]; [theString release];
现在我假设我需要设置一些属性。 我已经通过苹果的Cocoa文档看了一下,但是它有点压倒性,找不到如何将段落样式添加到属性中。
另外,我只能find水平alignment, 垂直alignment呢?
垂直alignment你必须自己做((视图的高度+string的高度)/ 2)。 您可以使用水平alignment方式:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; style.alignment = NSTextAlignmentCenter; NSDictionary *attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName]; [myString drawInRect:someRect withAttributes:attr];
这适用于我的水平alignment
[textX drawInRect:theRect withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
马丁斯的答案是非常接近,但它有一些小的错误。 尝试这个:
NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:NSCenterTextAlignment]; NSDictionary *attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName]; [myString drawInRect:someRect withAttributes:attr]; [style release];
您将不得不创build一个新的NSMutableParagraphStyle
(而不是使用默认的段落风格,因为马丁build议),因为[NSMutableParagraphStyle defaultParagraphStyle]
返回一个NSParagraphStyle
,它没有setAlignment方法。 此外,您不需要string@"NSParagraphStyleAttributeName"
只需NSParagraphStyleAttributeName
。
这适用于我:
CGRect viewRect = CGRectMake(x, y, w, h); UIFont* font = [UIFont systemFontOfSize:15]; CGSize size = [nsText sizeWithFont:font constrainedToSize:viewRect.size lineBreakMode:(UILineBreakModeWordWrap)]; float x_pos = (viewRect.size.width - size.width) / 2; float y_pos = (viewRect.size.height - size.height) /2; [someText drawAtPoint:CGPointMake(viewRect.origin.x + x_pos, viewRect.origin.y + y_pos) withFont:font];
对于任何对iOS7 +改编感兴趣的人,请将其放入NSString类别中:
- (void)drawVerticallyInRect:(CGRect)rect withFont:(UIFont *)font color:(UIColor *)color andAlignment:(NSTextAlignment)alignment { rect.origin.y = rect.origin.y + ((rect.size.height - [self sizeWithAttributes:@{NSFontAttributeName:font}].height) / 2); NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:alignment]; [self drawInRect:rect withAttributes:@{ NSFontAttributeName : font, NSForegroundColorAttributeName : color, NSParagraphStyleAttributeName : style }]; }
[NSMutableParagraphStyle defaultParagraphStyle]
将无法正常使用:
[NSMutableParagraphStyle new]
此外,它似乎水平alignment只适用于drawInRect
,而不是drawAtPoint
(问我怎么知道:-)
那么,drawInRect只适用于基本的文本绘制(换句话说,系统决定在哪里定位文本) – 通常绘制文本的唯一方法就是定位到你想要的位置,只需计算你想要的点,并使用NSString的drawAtPoint :withAttributes :.
另外,NSString的sizeWithAttributes在你最终必须为drawAtPoint执行的定位math中非常有用。
祝你好运!
正确的答案是:
-drawInRect:withFont:lineBreakMode:alignment:
我也创build了一个垂直alignment的小类。 如果你喜欢使用它,继续前进:)
// NSString+NSVerticalAlign.h typedef enum { NSVerticalTextAlignmentTop, NSVerticalTextAlignmentMiddle, NSVerticalTextAlignmentBottom } NSVerticalTextAlignment; @interface NSString (VerticalAlign) - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font verticalAlignment:(NSVerticalTextAlignment)vAlign; - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode verticalAlignment:(NSVerticalTextAlignment)vAlign; - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment verticalAlignment:(NSVerticalTextAlignment)vAlign; @end // NSString+NSVerticalAlign.m #import "NSString+NSVerticalAlign.h" @implementation NSString (VerticalAlign) - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font verticalAlignment:(NSVerticalTextAlignment)vAlign { switch (vAlign) { case NSVerticalTextAlignmentTop: break; case NSVerticalTextAlignmentMiddle: rect.origin.y = rect.origin.y + ((rect.size.height - font.pointSize) / 2); break; case NSVerticalTextAlignmentBottom: rect.origin.y = rect.origin.y + rect.size.height - font.pointSize; break; } return [self drawInRect:rect withFont:font]; } - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode verticalAlignment:(NSVerticalTextAlignment)vAlign { switch (vAlign) { case NSVerticalTextAlignmentTop: break; case NSVerticalTextAlignmentMiddle: rect.origin.y = rect.origin.y + ((rect.size.height - font.pointSize) / 2); break; case NSVerticalTextAlignmentBottom: rect.origin.y = rect.origin.y + rect.size.height - font.pointSize; break; } return [self drawInRect:rect withFont:font lineBreakMode:lineBreakMode]; } - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment verticalAlignment:(NSVerticalTextAlignment)vAlign { switch (vAlign) { case NSVerticalTextAlignmentTop: break; case NSVerticalTextAlignmentMiddle: rect.origin.y = rect.origin.y + ((rect.size.height - font.pointSize) / 2); break; case NSVerticalTextAlignmentBottom: rect.origin.y = rect.origin.y + rect.size.height - font.pointSize; break; } return [self drawInRect:rect withFont:font lineBreakMode:lineBreakMode alignment:alignment]; } @end
以下代码片段对于使用Annotation自定义图像作为参考来绘制中心文本string非常有用:
CustomAnnotation.h
@interface CustomAnnotation : MKAnnotationView [...]
CustomAnnotation.m
[...] - (void)drawRect:(CGRect)rect { ClusterAnnotation *associatedAnnotation = (CustomAnnotation *)self.annotation; if (associatedAnnotation != nil) { CGContextRef context = UIGraphicsGetCurrentContext(); NSString *imageName = @"custom_image.png"; CGRect contextRect = CGRectMake(0, 0, 42.0, 42.0); CGFloat fontSize = 14.0; [[UIImage imageNamed:imageName] drawInRect:contextRect]; NSInteger myIntegerValue = [associatedAnnotation.dataObject.myIntegerValue integerValue]; NSString *myStringText = [NSString stringWithFormat:@"%d", myIntegerValue]; UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize]; CGSize fontWidth = [myStringText sizeWithFont:font]; CGFloat yOffset = (contextRect.size.height - fontWidth.height) / 2.0; CGFloat xOffset = (contextRect.size.width - fontWidth.width) / 2.0; CGPoint textPoint = CGPointMake(contextRect.origin.x + xOffset, contextRect.origin.y + yOffset); CGContextSetTextDrawingMode(context, kCGTextStroke); CGContextSetLineWidth(context, fontSize/10); CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]); [myStringText drawAtPoint:textPoint withFont:font]; CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); [myStringText drawAtPoint:textPoint withFont:font]; } }
仅适用于iOS Swift 4 ,使用方法draw在矩形中居中string(in:withAttributes 🙂
let textFont = UIFont.boldSystemFont(ofSize: ) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center paragraphStyle.minimumLineHeight = rectangle.height / 2 + textFont.lineHeight / 2 let textAttributes = [ NSAttributedStringKey.font: textFont, NSAttributedStringKey.paragraphStyle: paragraphStyle ] as [NSAttributedStringKey : Any] let text = "Text" (text as NSString).draw(in: rectangle, withAttributes: textAttributes)