代码之家  ›  专栏  ›  技术社区  ›  LeeMobile

有没有一种方法可以使用iPhone上NSString的UIKit添加来绘制彩色文本?

  •  18
  • LeeMobile  · 技术社区  · 17 年前

    有没有一种方法可以使用iPhone上NSString的UIKit添加来绘制彩色文本?

    4 回复  |  直到 17 年前
        1
  •  50
  •   sabiland    17 年前

    如果你手头没有上下文(例如,如果你在drawRect:方法中这样做),Brad的解决方案会添加相当多的代码。

    没有CG的方法是

    [[UIColor redColor] set];
    

    或者你想要的任何颜色。

        2
  •  30
  •   Brad Larson    17 年前

    如果NSString UIKit添加的是类别方法drawAtPoint:和drawInRect:,那么您只需放置即可更改其绘制颜色

    CGContextSetFillColorWithColor(context, textColor);
    

    在调用drawAtPoint:或drawInRect:之前。手动绘制文本时,上下文的填充颜色用作文本的颜色。

        3
  •  1
  •   Dan Lorenc    17 年前

    aLabel = [[UILabel alloc] init];
    aLabel.text = @"My String";
    aLabel.textColor = [UIColor blackColor];
    
        4
  •  0
  •   Reed Olsen    17 年前

    在桌面上,您可以使用NSAttributedString,但这在iPhone上不可用。如果你只是想在屏幕上显示彩色文本,最简单的方法就是使用UILabel。您可以这样操作文本、字体和颜色:

    myLabel.text = @"Your text";
    myLabel.font = [UIFont boldSystemFontOfSize:16];
    myLabel.textColor = [UIColor redColor];