2

我有一个用代码绘制的字符串

[@"-" drawInRect: r withFont: f2 lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];

但是我想更改此字符串中的颜色,因为 NSString 无法做到这一点,我希望将其修改为 NSAttributedString。

但是这不起作用,这是我修改后的代码,我也找不到其他参数的属性

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:f2,NSFontAttributeName,
                                        nil];

    NSAttributedString *drawingString = [[NSAttributedString alloc]  initWithString:@"-" attributes:attrsDictionary];

    [drawingString drawInRect:r];

任何帮助表示赞赏这是否是正确的方法。

4

2 回答 2

2

谢谢!这是我最终编辑的代码

    NSMutableParagraphStyle *paraAttr = [[NSMutableParagraphStyle defaultParagraphStyle ] mutableCopy];
    [paraAttr setAlignment:UITextAlignmentCenter];
    [paraAttr setLineBreakMode:UILineBreakModeWordWrap];


    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:f2,NSFontAttributeName,
                                        [UIColor grayColor],NSForegroundColorAttributeName,
                                        paraAttr,NSParagraphStyleAttributeName,
                                         nil];


    NSAttributedString *drawingString = [[NSAttributedString alloc]  initWithString:@"•" attributes:attrsDictionary];


    [drawingString drawInRect:r];
于 2012-10-04T12:31:57.337 回答
1

您还需要设置NSForegroundColorAttributeName属性。

这是一个完整的列表- 在常量部分:

NSString *const NSFontAttributeName;
NSString *const NSParagraphStyleAttributeName;
NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;
NSString *const NSLigatureAttributeName;
NSString *const NSKernAttributeName;
NSString *const NSStrikethroughStyleAttributeName;
NSString *const NSUnderlineStyleAttributeName;
NSString *const NSStrokeColorAttributeName;
NSString *const NSStrokeWidthAttributeName;
NSString *const NSShadowAttributeName;
NSString *const NSVerticalGlyphFormAttributeName;
于 2012-10-04T11:53:00.243 回答