objective-c - 是否可以覆盖 UILabel 的 drawTextInRect 方法以更改 UILabel 的文本大小?

标签 objective-c iphone cocoa-touch core-graphics

Apple 的 drawTextInRect 文档似乎表明这是可能的:

"By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or you can do the drawing yourself. If you do render the text yourself, you should not invoke super."

但下面来 self 的 UILabel 子类的示例(我已经确认正在 被调用)不会导致文本大小发生变化,无论我指定什么文本大小。我是捕获了正确的上下文还是错过了更大的东西?

- (void)drawTextInRect:(CGRect)rect{

    CGContextRef theContext = UIGraphicsGetCurrentContext();
    CGContextSetFontSize(theContext, 32.0); // <-- doesn't change text size

    [super drawTextInRect:rect];
}

注意 - 文本大小并不是我唯一需要更改的文本,但如果我可以更改文本大小,我很确定我需要进行的其余更改会很容易。

最佳答案

/* use CGContextSetLineWidth() and CGContextSetStrokeColorWithColor() 
 to adjust your outline settings: directly preceding StrokeRect() */


- (void)drawTextInRect:(CGRect)rect{

    [super drawTextInRect:rect]; // let super do the work

    CGContextStrokeRect(UIGraphicsGetCurrentContext(),rect);
}

关于objective-c - 是否可以覆盖 UILabel 的 drawTextInRect 方法以更改 UILabel 的文本大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1108440/

相关文章:

objective-c - 使用推送动画查看交换

objective-c - iOS 的加密 NSString AES256EncryptWithKey 和 AES256DecryptWithKey

iphone - iOS ping 和 ttl 超时

ios - ipad 和 iphone 的自适应大小类解决方案

iphone - 不是从 appDelegate 添加 UITabBarController 吗?

ios - CATransform3D 打破图层顺序?

objective-c - Ruby 1.8.7 中的 Ruby 字符串编码

ios - SceneKit 中的反重力物理

iphone - 如何查看Core Data中已有的数据?

objective-c - -[国家保留] : message sent to deallocated instance 0x6829280