2012-10-04 38 views
2

我有被畫成與代碼轉換的NSString NSAttribtuedString繪製

[@"-" 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]; 

任何幫助表示讚賞,這是否是這樣做的正確方法。

回答

1

您還需要設置NSForegroundColorAttributeName屬性。

這裏有一個complete list - 在常量節:

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; 
+0

任何想法如何複製textalignment和linebreakmode屬性? – Prat

+0

@Prat不,我不後悔。 – trojanfoe

+1

啊我發現它在NSParagraphStyle,謝謝! – Prat

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];