objective-c - iOS 模糊文本 : detecting & solving it once and for all?

标签 objective-c reflection ios uiview swizzling

我不止一次遇到 UIView(子类)以分数偏移结束的情况,例如因为它的尺寸是奇数且居中,或者因为它的位置基于奇数大小容器的中心。

这会导致文本(或图像)模糊,因为 iOS 会尝试在半像素偏移上渲染 View (和 subview )。我觉得为每次帧更改调用 CGRectIntegral() 并不是一个完美的解决方案。

我正在寻找轻松检测这些情况的最佳方法。在写这个问题时,我想出了一个非常激进的方法,它揭示了我当前项目中的 ½ 偏差比我想象的要多。

所以这是为了分享。非常欢迎对更好或更温和的替代方案提出意见和建议。

主.m

#import <objc/runtime.h>
#import "UIViewOverride.h"

int main(int argc, char *argv[]) {

#ifdef DEBUGVIEW
    Method m1,m2;
    IMP imp;
    m1 = class_getInstanceMethod([UIView class], @selector(setFrame:));
    m2 = class_getInstanceMethod([UIViewOverride class], @selector(setFrameOverride:));
    imp = method_getImplementation(m2);
    class_addMethod([UIView class], @selector(setFrameOverride:), imp, method_getTypeEncoding(m1));
    m2 = class_getInstanceMethod([UIView class], @selector(setFrameOverride:));
    method_exchangeImplementations(m1,m2);

    m1 = class_getInstanceMethod([UIView class], @selector(setCenter:));
    m2 = class_getInstanceMethod([UIViewOverride class], @selector(setCenterOverride:));
    imp = method_getImplementation(m2);
    class_addMethod([UIView class], @selector(setCenterOverride:), imp, method_getTypeEncoding(m1));
    m2 = class_getInstanceMethod([UIView class], @selector(setCenterOverride:));
    method_exchangeImplementations(m1,m2);
#endif

// etc

UIViewOverride.m

这是作为 UIView 子类实现的,它避免了转换和/或编译器警告。

#define FRACTIONAL(f) (fabs(f)-floor(fabs(f))>0.01)

@implementation UIViewOverride

#ifdef DEBUGVIEW
-(void)setFrameOverride:(CGRect)newframe
{
    if ( FRACTIONAL(newframe.origin.x) || FRACTIONAL(newframe.origin.y) )
    {
        [self setBackgroundColor:[UIColor redColor]];
        [self setAlpha:0.2];
        NSLog(@"fractional offset for %@",self);
    }
    [self setFrameOverride:newframe]; // not a typo
}

-(void)setCenterOverride:(CGPoint)center
{
    [self setCenterOverride:center]; // not a typo
    if ( FRACTIONAL(self.frame.origin.x) || FRACTIONAL(self.frame.origin.y) )
    {
        [self setBackgroundColor:[UIColor greenColor]];
        [self setAlpha:0.2];
        NSLog(@"fractional via center for %@",self);
    }
}
#endif

有问题的 View 将生成日志消息并变成透明的,或者是红色的,或者是绿色的。

-DDEBUGVIEW 在 Debug模式下设置为编译器标志。

最佳答案

您可以通过 CoreAnimation 工具及其未对齐标志获得相同的功能。

关于objective-c - iOS 模糊文本 : detecting & solving it once and for all?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637488/

相关文章:

ios - 如何在 Json 请求中的参数中传递一个 ID 数组

ios - Swift4:从文件解码json时出错

java - Scala 反射 : why getMethods can return the val members?

go - 如何使用反射将结构值转换为结构指针

Java 反射 : How can I retrieve anonymous inner classes?

ios - 使用其他三个 View Controller 设置 UITabBarController

iphone - 我如何刷新/重新加载以更新对我的谓词的更改并因此获取请求?

ios - 滚动直到元素可见 iOS UI Automation with Xcode 7

iphone - 如何将文件从 URL 复制到文档文件夹?

iOS 8 UIView 没有得到更新的问题