gpt4 book ai didi

iphone - 为 UITextView 添加轮廓/描边效果 ios <7

转载 作者:行者123 更新时间:2023-12-03 19:51:39 27 4
gpt4 key购买 nike


我正在寻找向 UITextView 内的文本添加轮廓/描边的解决方案
对于 UILabel,我可以通过覆盖 - (void)drawTextInRect:(CGRect)rect 轻松做到这一点
我还找到了一些解决方案,但它们对我不起作用:
- 对于 iOS 7,我发现可以通过使用 NSString 方法来解决这个问题:drawInRect:rect withAttributes: 像这样

- (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 的解决方案?谢谢

最佳答案

我更新了也在寻找这个问题的人的答案。
子类化 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);
}

关于iphone - 为 UITextView 添加轮廓/描边效果 ios <7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com