是否有方法在矩形的中间绘制文本.我可以找到各种对齐方式,但我尝试过的任何内容都不能将文本垂直居中.
有没有一种简单的方法可以做到这一点,或者是否有某种方法可以使矩形居中然后绘制?
我正在直接绘制CGContext,尝试使用NSString :: drawWithRect或类似的东西,因为我真的不想添加一个Label来呈现一些基本文本.
Ama*_*mer 33
好吧,font属性pointSize直接对应于NSString绘制的高度(以像素为单位)UIFont,因此您的公式将是这样的:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
UITextAlignmentCenter处理horizontal居中,所以我们使用contextRect的全宽.在lineBreakMode可以任何你喜欢的.
Blu*_*ish 18
这是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];
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5
static func draw(_ text: String, _ rect: CGRect, _ font: UIFont) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: UIColor.red
]
NSAttributedString(string: text, attributes: attributes).draw(in: rect.insetBy(dx: 0, dy: (rect.height - font.pointSize)/2))
}
Run Code Online (Sandbox Code Playgroud)
如果要绘制多条线,
func drawMutipleLine(text: String = "Good morning how \nare you , fine, thank\nyou good day. uck wood?",
_ rect: CGRect,
font: UIFont = UIFont(name: "HelveticaNeue-Thin", size: 15)!) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: rect.size)
let image = renderer.image { ctx in
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let font = UIFont(name: "HelveticaNeue-Thin", size: 15)!
let attributes = [NSAttributedString.Key.font:font, NSAttributedString.Key.paragraphStyle: paragraphStyle]
let numLines = text.split(separator: "\n").count + 1
NSAttributedString(string: text, attributes: attributes).draw(in: rect.insetBy(dx: 0, dy: (rect.height - font.pointSize * CGFloat(numLines))/2))
}
return image
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37155 次 |
| 最近记录: |