1

ユーザーがDONEを押した後、テキストをUIImageViewに変換して表示します。1行のテキストで非常にうまく機能し、

およびスクリーンショット 1

. しかし、ユーザーが 2 行以上入力した場合: 結果は 1 行のままです??? スクリーンショット 2

UIimageViewで複数行表示したい

誰でも私を助けることができます!どうもありがとうございました!

これが私のコードです:

-(UIImage *)convertTextToImage : (ObjectText *) objT; 
{ 
    UIGraphicsBeginImageContext(CGSizeMake(([objT.content sizeWithFont:objT.font].width+10), ([objT.content sizeWithFont:objT.font].height+10)));
    [[objT getcolor] set];
    [objT.content drawAtPoint:CGPointMake(5, 5) withFont:objT.font];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();     
    UIGraphicsEndImageContext(); 
    return result; 
}
4

3 に答える 3

0

抽象化された属性

NSString* textContent = @"Hello, World!";
NSString* text2Content = @"Hello, World!"; 
NSString* text3Content = @"Hello, World!"; 
NSString* text4Content = @"Hello, World!";

テキスト描画-1

CGRect textRect = CGRectMake(65, 43, 56, 55);
[[UIColor blackColor] setFill];
[textContent drawInRect: textRect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];

テキスト描画-2

CGRect text2Rect = CGRectMake(77, 79, 37, 31);
[[UIColor blackColor] setFill];
[text2Content drawInRect: text2Rect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];

テキスト描画-3

CGRect text3Rect = CGRectMake(118, 74, 49, 43);
[[UIColor blackColor] setFill];
[text3Content drawInRect: text3Rect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];

テキスト描画-4

CGRect text4Rect = CGRectMake(71, 17, 41, 28);
[[UIColor blackColor] setFill];
[text4Content drawInRect: text4Rect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];
于 2012-12-08T05:02:16.327 に答える
0

これが私の答えです。良い方法だと思います。このコードを試してください:

-(UIImage *)convertTextToImage : (ObjectText *) objT; { UIGraphicsBeginImageContext(CGSizeMake([objT.content sizeWithFont:objT.font constraintToSize:CGSizeMake(900, 900)].width+10, [objT.content sizeWithFont:objT.font constraintToSize:CGSizeMake(900, 900)].height+10 ));

[[objT getcolor] set];

[objT.content drawInRect:CGRectMake(5, 5, [objT.content sizeWithFont:objT.font constrainedToSize:CGSizeMake(900, 900)].width, [objT.content sizeWithFont:objT.font constrainedToSize:CGSizeMake(900, 900)].height) withFont:objT.font];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return  result;

}

于 2012-12-08T10:40:28.563 に答える
0

sizeWithFont問題は、ラベルのサイズを完全に制御する方法に依存していることです。幅を制限しない場合、ラベルは常に、テキストが 1 行に表示されるのに十分な幅のフレームに拡張されます。

これを試してください(コードは幅を指定した幅に制限します):

-(UIImage *)convertTextToImage : (ObjectText *) objT; 
{ 
    //THIS IS THE MAX WIDTH YOU WANT, and anything wider would wrap to two lines
    CGFloat desiredWidth = 100.0;
    CGSize sizeToConstrainTo = CGSizeMake(desiredWidth, 5000);
    CGSize newSize = [objT.content sizeWithFont:objT.font
                              constrainedToSize:sizeToConstrainTo
                                  lineBreakMode:UILineBreakModeWordWrap];
    UIGraphicsBeginImageContext(newSize);
    [[objT getcolor] set];
    [objT.content drawAtPoint:CGPointMake(5, 5) withFont:objT.font];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();     
    UIGraphicsEndImageContext(); 
    return result; 
}
于 2012-12-08T03:36:17.530 に答える