开发者

Add text to UIImageView

i have a core data model that contain string, i want that when i select a row of the table contain the string of the core data i see in other view the text printed in a UIImage, i have done all, and i found this method: http://iphonesdksnippets.com/post/2009/05/05/Add-text-to-image-%28UIImage%29.aspx, but when i print the test, he print it in the left bottm corner of the UIImageView...in the (0,0) point, how i can make print the text in the middle of the image and justified automatically the text?, because the image is a triangle, and i want the text stay in the triangle, how i can do it? or there is another method?...thanks!

Ple开发者_如何转开发ase help me...


It may be easier to use a UIImageView and a separate `UILabel1 to display the text. I think you would have more control over the positioning.


- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)myImage {
UIImage *myImage = [UIImage imageNamed:@"promoicon.png"];
UIGraphicsBeginImageContext(myImage.size);
[myImage drawInRect:CGRectMake(0,0,myImage.size.width,myImage.size.height)];
UITextView *myText = [[UITextView alloc] init];
myText.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15.0f];
myText.textColor = [UIColor whiteColor];
myText.text = NSLocalizedString(@"My Text", @"");
myText.backgroundColor = [UIColor clearColor];
CGSize maximumLabelSize = CGSizeMake(myImage.size.width,myImage.size.height);
CGSize expectedLabelSize = [myText.text sizeWithFont:myText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
myText.frame = CGRectMake((myImage.size.width / 2) - (expectedLabelSize.width / 2),(myImage.size.height / 2) - (expectedLabelSize.height / 2),myImage.size.width,myImage.size.height);
[[UIColor whiteColor] set];
[myText.text drawInRect:myText.frame withFont:myText.font];
UIImage *myNewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return myNewImage; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜