【问题标题】:Adding text to image, trying to change the color of text向图像添加文本,尝试更改文本的颜色
【发布时间】:2014-07-04 07:21:53
【问题描述】:

我正在使用下面提到的代码向图像添加文本。但我面临的问题是颜色和字体大小没有改变。我正在尝试更改字体大小,但没有发生并且不知道如何更改颜色。

-(UIImage *)imageFromText:(NSString *)text
{
// set the font type and size
//UIFont *font = [UIFont systemFontOfSize:txtView.font];
//CGSize size  = [text sizeWithFont:[UIFont fontWithName:@"Arial" size:13.0f]]; // label or textview
NSDictionary *attributes = @{
                             NSFontAttributeName: [UIFont fontWithName:@"Arial" size:14]
                             };
CGSize size = [text sizeWithAttributes:@{
                                         NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:14]
                                         }];

// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
    // iOS is < 4.0
    UIGraphicsBeginImageContext(size);

//[text drawInRect:CGRectMake(0,0,size.width,size.height) withFont:[UIFont fontWithName:@"Arial" size:15.0f]];
CGRect drawRect = CGRectMake(0.0, 0.0, size.width,size.height);

NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
drawingContext.minimumScaleFactor = 1.5;

[text drawWithRect:drawRect options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:drawingContext];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}

请指导,为什么字体大小没有改变,代码有什么问题以及如何改变这个文本的颜色。

【问题讨论】:

    标签: iphone ipad ios7


    【解决方案1】:

    我终于找到了自己。

    [theText drawAtPoint:CGPointMake(x, y)
                  withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:8], NSForegroundColorAttributeName:[UIColor whiteColor] }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 2016-09-17
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多