2013-10-01 61 views
3


我正在尋找解決方案,以增加輪廓/筆畫一個UITextView內
有關的UILabel,我可以很容易地覆蓋爲此- (void)drawTextInRect:(CGRect)rect
我也找到了一些解決方案,但他們並沒有爲我工作:
- 對於iOS 7,我發現這可以通過使用NSString方法來解決:drawInRect:rect withAttributes:這樣
的文本添加大綱/行程作用的UITextView IOS <7

- (void)drawRect:(CGRect)rect 
{ 
    NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary]; 

    // Define the font and fill color 
    [stringAttributes setObject: self.font forKey: NSFontAttributeName]; 
    [stringAttributes setObject: self.textColor forKey: NSForegroundColorAttributeName]; 
    // Supply a negative value for stroke width that is 2% of the font point size in thickness 
    [stringAttributes setObject: [NSNumber numberWithFloat: -2.0] forKey: NSStrokeWidthAttributeName]; 
    [stringAttributes setObject: self.strokeColor forKey: NSStrokeColorAttributeName]; 

    // Draw the string 
    [self.text drawInRect:rect withAttributes:stringAttributes]; 
} 

是否有可能爲iOS < 7所支持的任何解決方案? 謝謝

回答

2

我更新了一個人也在尋找這個問題的答案。
子類UITextView並覆蓋像這樣的drawRect函數

- (void)drawRect:(CGRect)rect 
{ 
    [super drawRect:rect]; 

    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:rect.size lineBreakMode:NSLineBreakByWordWrapping]; 
    CGRect textRect = CGRectMake((rect.size.width - size.width)/2,(rect.size.height - size.height)/2, size.width, size.height); 

    //for debug 
    NSLog(@"draw in rect: %@", NSStringFromCGRect(rect)); 
    NSLog(@"content Size : %@", NSStringFromCGSize(self.contentSize)); 
    NSLog(@"Text draw at :%@", NSStringFromCGRect(textRect)); 

    CGContextRef textContext = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(textContext); 
    //set text draw mode and draw the stroke 
    CGContextSetLineWidth(textContext, 2); // set the stroke with as you wish 
    CGContextSetTextDrawingMode (textContext, kCGTextStroke); 

    CGContextSetStrokeColorWithColor(textContext, [UIColor blackColor].CGColor); 

    [self.text drawInRect:textRect withFont:self.font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter]; 
    CGContextRestoreGState(textContext); 
}