是否有方法在矩形中間繪製文字?我可以找到各種路線,但我沒有試過可以垂直居中文本的矩形。iPhone - 如何在矩形中間繪製文字
是否有一個簡單的方法來做到這一點,或者有一種方法來居中一個矩形,然後在那裏畫?
我正在直接繪製CGContext,嘗試使用NSString :: drawWithRect或類似的東西,因爲我真的不想添加一個Label來呈現一些基本文本。
是否有方法在矩形中間繪製文字?我可以找到各種路線,但我沒有試過可以垂直居中文本的矩形。iPhone - 如何在矩形中間繪製文字
是否有一個簡單的方法來做到這一點,或者有一種方法來居中一個矩形,然後在那裏畫?
我正在直接繪製CGContext,嘗試使用NSString :: drawWithRect或類似的東西,因爲我真的不想添加一個Label來呈現一些基本文本。
好,字體屬性pointSize
直接對應於高度在UIFont
繪製的NSString
的像素,讓您的公式是這樣的:
- (void) drawString: (NSString*) s
withFont: (UIFont*) font
inRect: (CGRect) contextRect {
CGFloat fontHeight = font.pointSize;
CGFloat yOffset = (contextRect.size.height - fontHeight)/2.0;
CGRect textRect = CGRectMake(0, yOffset, contextRect.size.width, fontHeight);
[s drawInRect: textRect
withFont: font
lineBreakMode: UILineBreakModeClip
alignment: UITextAlignmentCenter];
}
UITextAlignmentCenter
處理horizontal
中心,所以我們使用contextRect的全部寬度。 lineBreakMode
可以是任何你喜歡的。
在iOS 6中,您可以獲得屬性字符串和段落對齊,因此這變得更加直接和強大。在這個例子中,我們在一個矩形中間畫一個大大的紅「A」:
NSMutableParagraphStyle* p = [NSMutableParagraphStyle new];
p.alignment = NSTextAlignmentCenter;
NSAttributedString* bigA =
[[NSAttributedString alloc] initWithString:@"A" attributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Georgia" size:100],
NSForegroundColorAttributeName: [UIColor redColor],
NSParagraphStyleAttributeName: p
}];
[bigA drawInRect:CGRectMake(0,0,200,200)];
水平居中文本,但不垂直對齊。 – Clay 2012-11-15 18:57:57
「垂直對齊」?只要把它畫在你想要的地方。或者將它繪製在某個物體(例如圖像)中,然後將其放在需要的地方。 – matt 2012-11-15 19:05:49
對 - 我明白你在說什麼。我只是想指出,我認爲這個問題是關於如何在更大的東西的中心垂直排列某些東西。例如,給定一個預定的'NSRect',如何使用'[myString drawInRect:myRect]'或一個變體來將字符串定位在'myRect'的垂直中心。對於Cocoa來說,我通過在https://github.com/jerrykrinock/CategoriesObjC中包括'NS(Attributed)String + Geometrics'類,並使用' - (float)heightForWidth:(float)寬度「方法。 – Clay 2012-11-15 23:51:52
我同意amagrammer的答案,除了一個小的變化:我使用font.lineSize
得到適當的高度。我認爲sizeWithFont
會給你LINESIZE以及
我同意你的意見,但lineSize或lineHeight? – damithH 2017-12-03 01:30:20
我發現這個代碼工作得很好:
NSString *text = @"A bit of text to draw";
UIFont *font = [UIFont systemFontOfSize:12];
CGRect textFrame = (CGRect)
{
.size.width = 150,
.size.height = 150,
};
CGSize textSize = [text sizeWithFont:font constrainedToSize:textFrame.size lineBreakMode:NSLineBreakByWordWrapping];
CGRect newTextFrame = CGRectInset(textFrame, 0, (textFrame.size.height - textSize.height)/2);
[text drawInRect:newTextFrame withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
的文字,字體和含幀可以來自任何地方,所以只是將相同線路當您進行計算和繪圖時使用中斷模式。
這是iOS7.0 +的更新版本。
我也對上述答案進行了改進,方法是使用sizeWithAttributes:
方法,它返回文本的邊界,使您能夠在繪製文本之前正確定位文本。這消除了對任何調整進行硬編碼的需要,如果使用不同大小的字體或使用不同的字體,這些調整將會中斷。
- (void) drawString: (NSString*) s
withFont: (UIFont*) font
inRect: (CGRect) contextRect {
/// 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 = NSTextAlignmentCenter;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSForegroundColorAttributeName: [UIColor whiteColor],
NSParagraphStyleAttributeName: paragraphStyle };
CGSize size = [s sizeWithAttributes:attributes];
CGRect textRect = CGRectMake(contextRect.origin.x + floorf((contextRect.size.width - size.width)/2),
contextRect.origin.y + floorf((contextRect.size.height - size.height)/2),
size.width,
size.height);
[s drawInRect:textRect withAttributes:attributes];
}
這非常接近我最終的結果。謝謝 – Xetius 2009-08-20 18:43:48
不客氣。如果我可能會問,你改變了什麼? – Amagrammer 2009-08-20 20:51:03
嗨,我知道這是相當古老的,但我只是用它,不得不做出改變。 我的更改是CGRect textRect ...行。我將它改爲... CGRect textRect = CGRectMake(contextRect.origin.x,contextRect.origin.y + yOffSet,contextRect.size.width,fontHeight); – Fogmeister 2012-03-16 22:05:30