Burada iOS7.0 + için güncellenmiş bir sürüm var.
Ayrıca, metni çizmeden önce uygun şekilde konumlandırmanızı sağlayan metnin sınırlarını döndüren sizeWithAttributes:
yöntemini kullanarak da yukarıdaki yanıtı geliştirdim. Bu, farklı bir boyut yazı tipi kullanırsanız veya farklı bir yazı tipi kullanırsanız kırılacak tüm ayarların kodlanması ihtiyacını ortadan kaldırır.
- (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];
}
Bu, neyle gittiğime çok yakın. Teşekkürler – Xetius
Bir şey değil. Ne sorabilirdin, sorabilir miyim? – Amagrammer
Merhaba, bunun oldukça eski olduğunu biliyorum ama sadece kullandım ve bir değişiklik yapmak zorundaydım. Değişikliğim, CGRect textRect ... satırıydı. Bunu değiştirdim ... CGRect textRect = CGRectMake (contextRect.origin.x, contextRect.origin.y + yOffSet, contextRect.size.width, fontHeight); – Fogmeister